Friday 5 May 2017

router.use middleware chain not working as expected?

i've the following middlewares:

injectParamMiddleware.js

module.exports = function(req, res, next) {
  req.params.specialParam = 'specialValue';
  next();
}

logParamMiddleware.js

module.exports = function(req, res, next) {
  console.log(req.params.specialParam);
  next();
}

SCENARIO 1

router.get('/resource/:resourceId', 
           injectParamMiddleware,
           logParamMiddleware,
           ...MoreMiddlewares)

SCENARIO 2

router.use('/resource/:resourceId', injectParamMiddleware, logParamMiddleware);
router.get('/resource/:resourceId', ...MoreMiddlewares);

While running SCENARIO 1, i get specialValue logged in the console. While running SCENARIO 2, i get undefined logged in the console.

I was expecting SCENARIO 2 to log specialValueas well.

Any help is really appreciated. Thanks in advance.

EDIT: I'm using Express 4.14.0 and Node 6.10.3



via MerLito

No comments:

Post a Comment