我正在尝试使用koa-router来定义不同的路由,并且我花了很长时间让它工作起来。
如下所示:
const apiRouter = new KoaRouter({
prefix: '/api'
})
.use(bodyParser)
.post('/sign-in', signinMiddleware)
.get('auth-check', authCheckMiddleware)
const protectedApisRouter = new KoaRouter()
.use(authorizeMiddleware)
.get('/widgets', getWidgetsListMiddleware)
.post('/widgets', createWidgetMiddleware)
.get('/widgets/:widgetId', getWidgetByIdMiddleware)
.patch('/widgets/:widgetId', updateWidgetMiddleware)
apiRouter.use(
prodectedApisRouter.routes(),
prodectedApisRouter.allowedMethods()
)
koaApp.use(apiRouter.routes())
koaApp.use(apiRouter.allowedMethods())
我期望在bodyParser和authorizeMiddleware中间件运行之后,对/api/widgets/*的请求应该进入它们各自的中间件,基于这里的文档:https://github.com/alexmingoia/koa-router#nested-routers
但是相反,我得到了所有这些路线的404分。我做错了什么?
发布于 2018-01-20 06:11:13
显然上面的代码运行得很好..但在我的authorizeMiddleware中,我使用的是await next而不是await next()?
太糟糕了,这里没有删除问题的方法。现在人们来这里是为了解决与我的愚蠢无关的问题。
https://stackoverflow.com/questions/48349795
复制相似问题