我正在尝试使用一组中间件。好吧,更像是函数名和数组的组合。
而不是:
router.post('/editPassword', validate, changePassword, sendConfirmation);
我想要这样的东西:
router.post('/editPassword', validate, [changePassword, sendConfirmation] );
那看起来像:
router.post('/editPassword', validate, doAction );
doAction
是这样的数组:
var doAction = [
//equivalent of changePassword
function(req, res, next){
//whatever
next();
},
//equivalent to the previous sendConfirmation
function(req, res, next){
//whatever
}
]
但它似乎失败了,并返回到 doAction
中第一个函数中的 next()
之后的 validate
步骤。
我正在寻找一种方法来简化中间件链接,包括一些同名的中间件步骤。
最佳答案
最新版本的 Express 可以处理这个问题:
function logOriginalUrl (req, res, next) {
console.log('Request URL:', req.originalUrl)
next()
}
function logMethod (req, res, next) {
console.log('Request Type:', req.method)
next()
}
var logStuff = [logOriginalUrl, logMethod]
app.get('/user/:id', logStuff, function (req, res, next) {
res.send('User Info')
})
您可以从 this link 查看更多内容
关于javascript - 在 express.js 中使用一组中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42859020/