我有一个定义如下的服务器:
app.get('/', function(req, res) {
// gets something
}
app.post('/', function(req, res) {
// updates something, need to be authenticated
}
现在我希望该post操作仅适用于经过身份验证的用户,所以我想auth在他们之间添加一个中间件,如下所示:
app.get('/', function(req, res) {
// gets something
}
app.use('/', function(req, res) {
// check for authentication
}
app.post('/', function(req, res) {
// updates something, need to be authenticated
}
GET通过这种方式,POST用户必须通过身份验证。
问题是 express 没有进入我的app.use中间件。如果我将app.use中间件放在所有app.VERB路由之前,它就可以工作。
有没有办法像我想要的那样做?