javascript - node - ¿Cómo envolver varias funciones de middleware en una sola?
node express use (1)
Tengo varias funciones de middleware similares a las siguientes:
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'});
}
¿Hay alguna manera de envolverlos en una función? Entonces haría algo como:
function respondIfValid(req, res, next) {
// Evoke the following middleware:
// validate
// checkValid
// respond
}
app.use('/', respondIfValid);
En lugar de:
app.use('/', validate, checkValid, respond);
prueba con el siguiente código
app.use('/', [validate, checkValid,respond]);
O
var middleware = [validate, checkValid,respond];
app.use('/', middleware );
Necesita colocar todas las funciones en esa serie como su requisito de ejecución.
Gracias