在Express上使用带有“app.Use”的路线参数

在Express上使用带有“app.Use”的路线参数,express,routeparams,Express,Routeparams,我无法在应用程序中使用路线参数。请在Express 4.16.2中使用 我有这样一句话: app.use('/course-sessions/:courseSessionId/chapter-sessions', chapterSessionsRouter) app.get('/course-sessions/:courseSessionId/chapter-sessions', ...chapterSessionResource.indexMethods) 并且,在第章中: // Inde

我无法在
应用程序中使用路线参数。请在Express 4.16.2中使用

我有这样一句话:

app.use('/course-sessions/:courseSessionId/chapter-sessions', chapterSessionsRouter)
app.get('/course-sessions/:courseSessionId/chapter-sessions', ...chapterSessionResource.indexMethods)
并且,在第章中:

// Index.
router.get('/', ...resource.indexMethods)
当我得到
'/course sessions/0/chapter sessions/'
时,我得到了404

然后我尝试这样一种说法:

app.use('/course-sessions/:courseSessionId/chapter-sessions', chapterSessionsRouter)
app.get('/course-sessions/:courseSessionId/chapter-sessions', ...chapterSessionResource.indexMethods)
现在我得到了我想要的结果。因此路线参数不适用于
应用程序。请使用


当我在研究的时候,我得到了答案,这就结束了这个问题。但问题似乎仍然存在。还是我做错了什么?

我只会使用应用程序。使用(“/course sessions/”,chapterSessionsRouter)并处理路由器内部的id。

您必须以不同的方式设置路由器,尝试使用
合并参数
访问父路由中的参数

let router = express.Router({ mergeParams: true });

请参阅文档:

我认为这是目前最合适的解决方案。但是如果
app.use
支持路由参数,或者
router.use
支持路由器作为中间件,那就太好了……谢谢,它成功了!它同时适用于
应用程序。使用
路由器。使用