【问题标题】:Redirect all other requests to index page将所有其他请求重定向到索引页面
【发布时间】:2018-10-29 15:35:45
【问题描述】:

来自https://github.com/koajs/jwt

app.use(jwt({ secret: 'shared-secret' }));

用于保护跟随的路线。

问题:如何只为定义的路由集指定它,并在没有匹配路径时重定向到静态 html 页面。

我当前的代码:

app
    .use(indexRouter.routes())
    .use(jwt({secret: conf.secret }))
    .use(protectedRouter.routes())
    .use((ctx: any)=> ctx.body='My html page'); <-- this doesn't work because it is considered a protected route

预期的伪代码:

app.use(indexRouter.routes())
        .use(protectedRouter.routes(), jwt({secret: conf.secret }))
        .use((ctx: any)=> ctx.body='My html page'); 

【问题讨论】:

    标签: node.js jwt koa


    【解决方案1】:

    由于我的受保护路由都在 'api' 路由下,所以我这样做了:

    .use(indexRouter.routes())
    .use(async (ctx: Context, next: () => void) => {
      if (ctx.path.substring(0,5) === '/api/'){
        await next();
      }else{
        ctx.body='My html page'
      }
    })
    .use(jwt({secret: conf.secret}))
    .use(protectedRouter.routes());
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多