1. 什麼是router路徑,什麼是middleware?express
咱們輸入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: 路徑爲 /testapp
www.baidu.com/test?name=1&number=2: 路徑一樣爲/test, ?後面會被服務器理解傳給路徑的參數。ide
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方法綁定,也能夠跟全部的方法綁定。post
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方法綁定:ui
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對象this
當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.