我正在尝试使用来定义不同的路线,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())
根据此处的文档,我希望在中间件运行之后,请求应该/api/widgets/*
进入各自的中间件: https ://github.com/alexmingoia/koa-router#nested-routersbodyParser
authorizeMiddleware
但相反,我为所有这些路线获得了 404。我究竟做错了什么?