日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

express 框架之 路由与中间件

發(fā)布時間:2023/12/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 express 框架之 路由与中间件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  1. ?什么是router路徑,什么是middleware?

?

我們輸入www.baidu.com 來訪問百度的主頁,瀏覽器會自動轉換為 http://www.baidu.com:80/(省略一些參數(shù))。 http://代表我們同服務器連接使用的是http協(xié)議,www.baidu.com 代表的是服務器的主機地址,會被我們的pc通過DNS解析為IP地址。80是默認的應用層端口。/ 即為我們訪問的服務器(www.baidu.com)的路徑,服務器要對我們訪問的這個路徑做出響應,采取一定的動作。我們可以把這一過程看做一個路由。

?訪問的路徑‘/’即為router的路徑,服務器采取的動作即為middleware,即為一個個特殊的函數(shù)。

?

  2. router路徑

?  www.baidu.com/test: 路徑為 /test

??????www.baidu.com/test?name=1&number=2: 路徑同樣為/test, ?后面會被服務器理解傳給路徑的參數(shù)。

?  3. Middleware?

An Express application is essentially a stack of middleware which are executed serially.(express應用其實就是由一系列順序執(zhí)行的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參數(shù)的函數(shù),這個函數(shù)可以訪問任何通過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.(如果當前中間件沒有完成對網(wǎng)頁的res響應 ,還可以通過next把router 留給下一個middleware繼續(xù)執(zhí)行) 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.

?

  路由的產(chǎn)生是通過HTTP的各種方法(GET, POST)產(chǎn)生的,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的路由路徑區(qū)別:

先看一段測試代碼:

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); View Code

輸入url:?localhost:4000

輸出結果:test1 輸入url: localhost:4000/hello 輸出結果:test2 結論:app.get掛載‘/’的路由只響應跟'/'精確匹配的GET請求。 而app.use掛載的'/'的路由響應所有以'/' 為起始路由的路由,且不限制HTTP訪問的方法。以下說明:Mounting a middleware at a path will cause the middleware function to be executed whenever the base of the requested path matches the path. 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.

?

轉載于:https://www.cnblogs.com/chenchenluo/p/4192282.html

總結

以上是生活随笔為你收集整理的express 框架之 路由与中间件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。