1. 什么是router路徑,什么是middleware?
我們輸入www.baidu.com 來訪問百度的主頁,瀏覽器會自動轉換為 http://www.baidu.com:80/(省略一些參數)。 http://代表我們同服務器連接使用的是http協議,www.baidu.com 代表的是服務器的主機地址,會被我們的pc通過DNS解析為IP地址。80是默認的應用層端口。/ 即為我們訪問的服務器(www.baidu.com)的路徑,服務器要對我們訪問的這個路徑做出響應,采取一定的動作。我們可以把這一過程看做一個路由。
訪問的路徑‘/’即為router的路徑,服務器采取的動作即為middleware,即為一個個特殊的函數。
2. router路徑
www.baidu.com/test: 路徑為 /test
www.baidu.com/test?name=1&number=2: 路徑同樣為/test, ?后面會被服務器理解傳給路徑的參數。
3. Middleware
An Express application is essentially a stack of middleware which are executed serially.(express應用其實就是由一系列順序執行的Middleware組成。) A middleware is a function with access to the request object (req), the response object (res), and the next middleware in line in the request-response cycle of an Express application. It is commonly denoted by a variable named next. Each middleware has the capacity to execute any code, make changes to the request and the reponse object, end the request-response cycle, and call the next middleware in the stack. Since middleware are execute serially, their order of inclusion is important.(中間件其實就是一個訪問express應用串入的req,res,nex參數的函數,這個函數可以訪問任何通過req,res傳入的資源。) If the current middleware is not ending the request-response cycle, it is important to call next() to pass on the control to the next middleware, else the request will be left hanging.(如果當前中間件沒有完成對網頁的res響應 ,還可以通過next把router 留給下一個middleware繼續執行) With an optional mount path, middleware can be loaded at the application level or at the router level. Also, a series of middleware functions can be loaded together, creating a sub-stack of middleware system at a mount point.
路由的產生是通過HTTP的各種方法(GET, POST)產生的,Middleware可以跟router路徑跟特定的HTTP方法綁定,也可以跟所有的方法綁定。
3.1 通過express應用的use(all),把Middleware同router路徑上的所有HTTP方法綁定:
1 app.use(function (req, res, next) { 2 console.log('Time: %d', Date.now()); 3 next(); 4 })
3.2 通過express應用的http.verb,把Middleware同router路徑上的特定的HTTP方法綁定:
1 app.get('/', function(req, res){ 2 res.send('hello world'); 3 }); 4 5 6 app.post('/', function(req, res){ 7 res.send('hello world'); 8 });
4. Express的Router對象
當express實例的路由越來越多的時候,最好把路由分類獨立出去,express的實例(app) 能更好的處理其他邏輯流程。Express的Router對象是一個簡化的 app實例,只具有路由相關的功能,包括use, http verbs等等。最后這個Router再通過app的use掛載到app的相關路徑下。
1 var express = require('express'); 2 var app = express(); 3 var router = express.Router(); 4 5 // simple logger for this router's requests 6 // all requests to this router will first hit this middleware 7 router.use(function(req, res, next) { 8 console.log('%s %s %s', req.method, req.url, req.path); 9 next(); 10 }); 11 12 // this will only be invoked if the path ends in /bar 13 router.use('/bar', function(req, res, next) { 14 // ... maybe some additional /bar logging ... 15 next(); 16 }); 17 18 // always invoked 19 router.use(function(req, res, next) { 20 res.send('Hello World'); 21 }); 22 23 app.use('/foo', router); 24 25 app.listen(3000);
router的路由必須通過app.use和app.verbs 掛載到app上才能被響應。所以上述代碼,只有在app捕捉到 /foo路徑上的路由時,才能router中定義的路由,雖然router中有針對 '/' 的路由,但是被app中的路由給覆蓋了。
附:app.verbs和app.use的路由路徑區別:
先看一段測試代碼:

1 var express = require('express'); 2 3 var app = express(); 4 var router = express.Router(); 5 6 app.get('/', function(req, res){ 7 console.log('test1'); 8 }); 9 10 app.use('/', function(req, res){ 11 console.log('test2'); 12 }); 13 14 router.get('/', function(req, res){ 15 console.log('test3'); 16 }); 17 18 app.listen(4000);
輸入url: localhost:4000
1 app.use([path], [function...], function) 2 Mount the middleware function(s) at the path. If path is not specified, it defaults to "/". 3 4 Mounting a middleware at a path will cause the middleware function to be executed whenever the base of the requested path matches the path.