我有一個具有以下定義的服務器:添加中間件之後app.router
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
,用戶必須進行身份驗證。
問題是,快遞不會進入我的app.use
中間件。如果我把app.use
中間件放在所有app.VERB
路由之前,它就可以工作。
有沒有辦法像我想要的那樣做?
我想知道是否因爲我明確2.x因爲我確定我有變化之前已經在'app.VERB()'後面定義了'app.use()'並且它工作正常 – Michael
@Michael用'express @ 2.5.1'進行了測試,似乎與3.x([gist ](https://gist.github.com/robertklep/bf45da5dc0a5947b237f)) – robertklep
你也可以使用app.all('*') – Micah