2013-01-02 20 views

回答

87

在大多數情況下,它們將起到等同作用。最大的區別是在中間件將應用的順序:

  • app.all()重視應用的路由器,所以每當達到app.router中間件它的使用(它處理所有的方法途徑... GET, POST等)。

  • app.use()附加到應用程序的主要中間件堆棧,因此它按中間件指定的順序使用。比如說,如果你把它放在第一位,這將是第一件事。如果你把它放在最後(在路由器之後),它通常不會運行。

通常,如果你想對全部路由做全局的事情,app.use()是更好的選擇。此外,它具有較少的未來bug的可能性,因爲express 0.4可能會降低隱式路由器(這意味着,路由器在中間件中的位置將比現在更重要,因爲您在技術上甚至不必使用它馬上)。

+10

這是否仍Express 4.x之後申請? app.router被刪除。 – ruffrey

+0

很好地解釋。 – user2045474

+0

你可以使用'next(「route」)''app'all',但不能'app.use'。 –

2

是,當請求與任何類型的請求方法(POST,GET,PUT或DELETE)

在另一方面app.use()用於任何中間件,你可能有它安裝特定URI app.all()被稱爲轉換爲路徑前綴,並且只要請求該路由下的URI就會被調用。

這是app.all的文檔& app.use

+0

謝謝,但我想你錯過了app.all通配符和app.use根路徑,使他們幾乎完全相同的事情不是嗎?除了app.all可以接受一系列回調,app.use只能採取一個 - 對嗎? – ostergaard

10

隨着app.use()中,「安裝」路徑被剝離,而不是到中間件功能可見:

app.use('/static', express.static(__dirname + '/public')); 

安裝中間件功能(express.static)不調用除非req.url包含此前綴(/static),在調用該函數時,該位置被剝離

With app.all(),沒有這種行爲。

+0

這個問題明確詢問app.use('/',...)。 – ostergaard

10
  • app.use:

    1. 注入middlware向前端控制器配置如:頭,餅乾,會話等
    2. 必須應用前寫[http_method]否則會有沒執行。
    3. 幾個調用的順序處理寫入
  • app.all的:

    1. (如應用[http_method])用於配置路由控制器
    2. 「所有」 的意思它適用於所有http方法。
    3. 幾個調用的順序處理這個expressJs代碼示例寫

的樣子:

var express = require('express'); 
var app = express(); 

app.use(function frontControllerMiddlewareExecuted(req, res, next){ 
    console.log('(1) this frontControllerMiddlewareExecuted is executed'); 
    next(); 
}); 

app.all('*', function(req, res, next){ 
    console.log('(2) route middleware for all method and path pattern "*", executed first and can do stuff before going next'); 
    next(); 
}); 

app.all('/hello', function(req, res, next){ 
    console.log('(3) route middleware for all method and path pattern "/hello", executed second and can do stuff before going next'); 
    next(); 
}); 

app.use(function frontControllerMiddlewareNotExecuted(req, res, next){ 
    console.log('(4) this frontControllerMiddlewareNotExecuted is not executed'); 
    next(); 
}); 

app.get('/hello', function(req, res){ 
    console.log('(5) route middleware for method GET and path patter "/hello", executed last and I do my stuff sending response'); 
    res.send('Hello World'); 
}); 

app.listen(80); 

下面是訪問路徑 '/你好' 時,日誌:

(1) this frontControllerMiddlewareExecuted is executed 
(2) route middleware for all method and path pattern "*", executed first and can do stuff before going next 
(3) route middleware for all method and path pattern "/hello", executed second and can do stuff before going next 
(5) route middleware for method GET and path patter "/hello", executed last and I do my stuff sending response 
+3

在Express 4.x上逐字運行此示例後,它實際上按順序運行全部5個示例。這可能是由於這篇文章寫了近3年後的表達變化,但我想我會補充說明。 –

47

app.use只需要一次回調fu它指的是中間件。中間件通常不處理請求和響應,(技術上他們可以)只處理輸入數據,並將其交給隊列中的下一個處理程序。

app.use([path], function) 

app.all需要多個回調,並意味着路由。通過多次回調,您可以過濾請求併發送回復。它在Filters on express.js

app.all(path, [callback...], callback) 

app.use解釋只能看到URL是否在指定的路徑

app.use("/product" , mymiddleware); 
// will match /product 
// will match /product/cool 
// will match /product/foo 

app.all開始將匹配完整路徑

app.all("/product" , handler); 
// will match /product 
// won't match /product/cool <-- important 
// won't match /product/foo <-- important 

app.all("/product/*" , handler); 
// won't match /product  <-- Important 
// will match /product/ 
// will match /product/cool 
// will match /product/foo 
+9

至少在[v4,app.use](http://expressjs.com/4x/api.html#app.use)中有一個*或多個*中間件功能,而不是「只有一個」。 –

+1

這應該是答案! – frogcjn

+2

app.use僅查看url是否以指定路徑開頭; app.all將匹配完整路徑。這是主要的區別。 – meizilp