rocketspacer rocketspacer - 15 days ago 6
Node.js Question

Get params of parent router

Assume that I have 2 routers nested together like so:

var appRouter = express.Router();
var childRouter = express.Router();

app.use('/:parentId/childpath', childRouter);
childRouter.get('/:childId', (req, res, next) => {
console.log(req.params); //only childId
// How do I get parentId ?
});


Is there any way to get
parentId
in sub-router handler ?

Answer

Create the child router with

express.Router({ mergeParams: true })
Comments