Encountered a strange bug where if I have a route defined like so in routes/posts/index.js:
遇到一个奇怪的错误,如果我在routes / posts / index.js中有这样的路由定义:
router.use('/:id/edit', require('./edit'));
Inside ./edit, I have the endpoint defined like so:
在./edit里面,我的端点定义如下:
router.post('/', passport.authenticate('jwt', { session: false}), function(req, res){
If I hit /posts/:id/edit, I successfully hit the endpoint in ./edit, but const id = req.params.id;
will be undefined. However, if I define the route directly in routes/posts/index.js, req.params.id
will be defined.
如果我点击/ posts /:id / edit,我在./edit中成功命中了端点,但是const id = req.params.id;将是未定义的。但是,如果我直接在routes / posts / index.js中定义路由,则将定义req.params.id。
What is the correct way for letting the route in ./edit 'recognize' the id parameter?
让./edit中的路由'识别'id参数的正确方法是什么?
1 个解决方案
#1
2
You can merge in parent router parameters by passing {mergeParams: true}
to express.Router()
.
您可以通过将{mergeParams:true}传递给express.Router()来合并父路由器参数。
#1
2
You can merge in parent router parameters by passing {mergeParams: true}
to express.Router()
.
您可以通过将{mergeParams:true}传递给express.Router()来合并父路由器参数。