You need to use the mergeParams(default: false)
option when creates a new router object.
Preserve the req.params
values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence.
So the working code is:
const express = require('express');
const app = express();
const { Router } = express;
const vote = Router({ mergeParams: true });
vote.get('/', (req, res) => {
console.log(req.params); //=> { id: '512333785338216465' }
res.sendStatus(200);
});
app.use('/bot/:id/vote', vote);
app.listen(3000, () => console.log('Server started at http://localhost:3000'));
package version: "express": "^4.17.1",