Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get params of parent router

Assume that I have 2 routers nested together like so:

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

appRouter.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 ?

like image 793
rocketspacer Avatar asked Nov 19 '16 13:11

rocketspacer


1 Answers

Create the child router with

express.Router({ mergeParams: true })
like image 85
Julian Avatar answered Oct 22 '22 22:10

Julian