Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass path parameters to a sub router in Express

For the code below:

router.use('/campaigns/:campaign_uid', cache.node_cache, api_dynamic_campaign.router);

I have a router route to another router which looks like this:

router.get('/data.json', campaigns);

The handler looks like this:

function campaigns(request, response){
    var campaign = api.get_campaign_from_request(request) || request.params.campaign_uid;
    api.route_handler(request, response, {"campaign":campaign});
}

My issue is the handler does not have access to campaign_uid in the request.params. How do I access/pass the campaign_uid to the handler?

like image 625
Brian Yeh Avatar asked Oct 31 '22 08:10

Brian Yeh


1 Answers

router.get('/data.json', campaigns).Router({ mergeParams: true })

https://cedric.tech/blog/expressjs-accessing-req-params-from-child-routers

like image 73
Trieu Avatar answered Nov 14 '22 14:11

Trieu