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);