Friday, 17 March 2017

Use an array of middlewares at express.js

I'm trying to use an array of middlewares. Well, more like a combination of function names and arrays.

Instead of having:

router.post('/editPassword', validate, changePassword, sendConfirmation);

I would like to have something like:

router.post('/editPassword', validate, [changePassword, sendConfirmation] ); 

That would look like:

router.post('/editPassword', validate, doAction ); 

Where doAction would be an array like this:

var doAction = [
   //equivalent of changePassword
   function(req, res, next){
      //whatever
      next();
   },

   //equivalent to the previous sendConfirmation
   function(req, res, next){
      //whatever
   }
]

But it seems it is failing and going back to the validate step after the next() within the first function in doAction.

I'm looking for a way to simplify the middleware chaining including some middleware steps under the same name.



via Alvaro

No comments:

Post a Comment