Node.JS Express中的app.all('*', ...)
和app.use('/', ...)
有什麼不同?app.all('*')和app.use('/')之間的區別
回答
在大多數情況下,它們將起到等同作用。最大的區別是在中間件將應用的順序:
app.all()
重視應用的路由器,所以每當達到app.router中間件它的使用(它處理所有的方法途徑... GET, POST等)。app.use()
附加到應用程序的主要中間件堆棧,因此它按中間件指定的順序使用。比如說,如果你把它放在第一位,這將是第一件事。如果你把它放在最後(在路由器之後),它通常不會運行。
通常,如果你想對全部路由做全局的事情,app.use()是更好的選擇。此外,它具有較少的未來bug的可能性,因爲express 0.4可能會降低隱式路由器(這意味着,路由器在中間件中的位置將比現在更重要,因爲您在技術上甚至不必使用它馬上)。
是,當請求與任何類型的請求方法(POST,GET,PUT或DELETE)
在另一方面app.use()
用於任何中間件,你可能有它安裝特定URI app.all()
被稱爲轉換爲路徑前綴,並且只要請求該路由下的URI就會被調用。
謝謝,但我想你錯過了app.all通配符和app.use根路徑,使他們幾乎完全相同的事情不是嗎?除了app.all可以接受一系列回調,app.use只能採取一個 - 對嗎? – ostergaard
隨着app.use()
中,「安裝」路徑被剝離,而不是到中間件功能可見:
app.use('/static', express.static(__dirname + '/public'));
安裝中間件功能(express.static
)不調用除非req.url
包含此前綴(/static
),在調用該函數時,該位置被剝離。
With app.all()
,沒有這種行爲。
這個問題明確詢問app.use('/',...)。 – ostergaard
app.use:
- 注入middlware向前端控制器配置如:頭,餅乾,會話等
- 必須應用前寫[http_method]否則會有沒執行。
- 幾個調用的順序處理寫入
app.all的:
- (如應用[http_method])用於配置路由控制器
- 「所有」 的意思它適用於所有http方法。
- 幾個調用的順序處理這個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
在Express 4.x上逐字運行此示例後,它實際上按順序運行全部5個示例。這可能是由於這篇文章寫了近3年後的表達變化,但我想我會補充說明。 –
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
- 1. app.all和app.use有什麼區別?
- 2. express.js中app.use和app.get之間的區別
- 3. 之間的〜/和的區別../
- 4. '#','%'和'$'之間的區別
- 5. {!!之間的區別!!}和{{}}
- 6. '+ ='和'= +'之間的區別?
- 7. 「。+」和「。+?」之間的區別
- 8. .eq之間的區別。和==
- 9. #。/和#之間的區別。 ./
- 10. !=和!之間的區別==
- 11. 「%〜dp0」和「。\」之間的區別?
- 12. | =和^ = css之間的區別
- 13. 之間的區別。和#
- 14. ==和case之間的區別?
- 15. 「**/* /」和「** /」之間的區別?
- 16. jQuery - '。'之間的區別和「#」
- 17. 「?1」和「?」之間的區別
- 18. `%in%`和`==`之間的區別
- 19. fmod和%之間的區別
- 20. 之間的區別:+和:+
- 21. 類區域和堆之間的區別
- 22. SPFile和PublishingPage類別之間的區別
- 23. 子類和類別之間的區別?
- 24. System.Timers.Timer Enabled = true和.Start()之間的區別Enabled = true和.Start()之間的區別
- 25. isForeground()和onExposed和onObscured之間的區別
- 26. $ .ajax()和$ .get()和$ .load()之間的區別
- 27. 和intent和setcontentview之間的區別
- 28. Java和C#和.NET之間的區別
- 29. NSthread和NStimer和NSNotifcation之間的區別?
- 30. sys.log()和console.log()和console.error()之間的區別?
這是否仍Express 4.x之後申請? app.router被刪除。 – ruffrey
很好地解釋。 – user2045474
你可以使用'next(「route」)''app'all',但不能'app.use'。 –