I have a number of middleware functions similar to the following:
function validate(req, res, next) {
  req.validationError = new Error('invalid');
}
function checkValid(req, res, next) {
  if (req.validationError) {
    next(req.validationError);
  } else {
    next();
  }
}
function respond() {
  res.json({result: 'success'});
}
Is there a way to wrap them into one function? So I'd do something like:
function respondIfValid(req, res, next) {
  // Evoke the following middleware: 
  // validate
  // checkValid
  // respond
}
app.use('/', respondIfValid);
Instead of:
app.use('/', validate, checkValid, respond);
                try with following code
app.use('/', [validate, checkValid,respond]);
OR
var middleware = [validate, checkValid,respond];
app.use('/', middleware );
Need to placed all function in that series as your requirement of execution.
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With