如何将路径参数传递给Express中的子路由器

时间:2016-03-03 22:33:55

标签: node.js express routing

对于以下代码:

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

我有一个到另一个路由器的路由器路由,如下所示:

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

处理程序如下所示:

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

我的问题是处理程序无法访问request.params中的campaign_uid。 如何访问/传递campaign_uid到处理程序?

1 个答案:

答案 0 :(得分:1)

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

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