2016-07-24 69 views
0

我想將中間件應用於路由,而不管請求的類型如何。目前,我有:申請中間件在Express中路由

router.route('/campaigns') 
    .get(restrictTo('advertiser'), restrictTo('admin'), queries.getCampaigns) 
    .post(restrictTo('advertiser'), restrictTo('admin'), queries.createCampaign); 

而且我想是這樣的:

router.route('/campaigns', restrictTo('advertiser'), restrictTo('admin')) 
    .get(queries.getCampaigns) 
    .post(queries.createCampaign); 

但這似乎並沒有工作。

回答

1

你應該能夠只使用.use()像這樣:

router.route('/campaigns') 
    .use(restrictTo('advertiser'), restrictTo('admin')) 
    .get(queries.getCampaigns) 
    .post(queries.createCampaign); 

或:

router.route('/campaigns') 
    .use(restrictTo('advertiser')) 
    .use(restrictTo('admin')) 
    .get(queries.getCampaigns) 
    .post(queries.createCampaign); 
+0

不工作.. – ocram

+0

應該是'。所有()','。用() '只適用於整個路由器,而不適用於單一路由。 – robertklep

相關問題