十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂(yōu)售后,網(wǎng)站問(wèn)題一站解決
小編給大家分享一下express實(shí)現(xiàn)中間件的原理分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)公司致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷(xiāo),提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、網(wǎng)站開(kāi)發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營(yíng)銷(xiāo)、重慶小程序開(kāi)發(fā)公司、公眾號(hào)商城、等建站開(kāi)發(fā),成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)策劃專(zhuān)家,為不同類(lèi)型的客戶(hù)提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶(hù)在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢(shì)。
簡(jiǎn)介
中間件機(jī)制可以讓我們?cè)谝粋€(gè)給定的流程中添加一個(gè)處理步驟,從而對(duì)這個(gè)流程的輸入或者輸出產(chǎn)生影響,或者產(chǎn)生一些中作用、狀態(tài),或者攔截這個(gè)流程。中間件機(jī)制和tomcat的過(guò)濾器類(lèi)似,這兩者都屬于責(zé)任鏈模式的具體實(shí)現(xiàn)。
express 中間件使用案例
let express = require('express') let app = express() //解析request 的body app.use(bodyParser.json()) //解析 cookie app.use(cookieParser()) //攔截 app.get('/hello', function (req, res) { res.send('Hello World!'); });
模擬中間件機(jī)制并且模擬實(shí)現(xiàn)解析request的中間件
首先模擬一個(gè)request
request = { //模擬的request requestLine: 'POST /iven_ HTTP/1.1', headers: 'Host:www.baidu.com\r\nCookie:BAIDUID=E063E9B2690116090FE24E01ACDDF4AD:FG=1;BD_HOME=0', requestBody: 'key1=value1&key2=value2&key3=value3', }
一個(gè)http
請(qǐng)求分為請(qǐng)求行、請(qǐng)求頭、和請(qǐng)求體,這三者之間通過(guò)\r\n\r\n
即一個(gè)空行來(lái)分割,這里假設(shè)已經(jīng)將這三者分開(kāi),requestLine
(請(qǐng)求行)中有方法類(lèi)型,請(qǐng)求url,http版本號(hào),這三者通過(guò)空格來(lái)區(qū)分,headers
(請(qǐng)求頭)中的各部分通過(guò)\r\n
來(lái)分割,requestBody
(請(qǐng)求體)中通過(guò) & 來(lái)區(qū)分參數(shù)
模擬中間件機(jī)制
約定 中間件一定是一個(gè)函數(shù)并且接受 request, response, next三個(gè)參數(shù)
function App() { if (!(this instanceof App)) return new App(); this.init(); } App.prototype = { constructor: App, init: function() { this.request = { //模擬的request requestLine: 'POST /iven_ HTTP/1.1', headers: 'Host:www.baidu.com\r\nCookie:BAIDUID=E063E9B2690116090FE24E01ACDDF4AD:FG=1;BD_HOME=0', requestBody: 'key1=value1&key2=value2&key3=value3', }; this.response = {}; //模擬的response this.chain = []; //存放中間件的一個(gè)數(shù)組 this.index = 0; //當(dāng)前執(zhí)行的中間件在chain中的位置 }, use: function(handle) { //這里默認(rèn) handle 是函數(shù),并且這里不做判斷 this.chain.push(handle); }, next: function() { //當(dāng)調(diào)用next時(shí)執(zhí)行index所指向的中間件 if (this.index >= this.chain.length) return; let middleware = this.chain[this.index]; this.index++; middleware(this.request, this.response, this.next.bind(this)); }, }
對(duì) request 處理的中間件
function lineParser(req, res, next) { let items = req.requestLine.split(' '); req.methond = items[0]; req.url = items[1]; req.version = items[2]; next(); //執(zhí)行下一個(gè)中間件 } function headersParser(req, res, next) { let items = req.headers.split('\r\n'); let header = {} for(let i in items) { let item = items[i].split(':'); let key = item[0]; let value = item[1]; header[key] = value; } req.header = header; next(); //執(zhí)行下一個(gè)中間件 } function bodyParser(req, res, next) { let bodyStr = req.requestBody; let body = {}; let items = bodyStr.split('&'); for(let i in items) { let item = items[i].split('='); let key = item[0]; let value = item[1]; body[key] = value; } req.body = body; next(); //執(zhí)行下一個(gè)中間件 } function middleware3(req, res, next) { console.log('url: '+req.url); console.log('methond: '+req.methond); console.log('version: '+req.version); console.log(req.body); console.log(req.header); next(); //執(zhí)行下一個(gè)中間件 }
測(cè)試代碼
let app = App(); app.use(lineParser); app.use(headersParser); app.use(bodyParser); app.use(middleware3); app.next();
整體代碼
function App() { if (!(this instanceof App)) return new App(); this.init(); } App.prototype = { constructor: App, init: function() { this.request = { //模擬的request requestLine: 'POST /iven_ HTTP/1.1', headers: 'Host:www.baidu.com\r\nCookie:BAIDUID=E063E9B2690116090FE24E01ACDDF4AD:FG=1;BD_HOME=0', requestBody: 'key1=value1&key2=value2&key3=value3', }; this.response = {}; //模擬的response this.chain = []; //存放中間件的一個(gè)數(shù)組 this.index = 0; //當(dāng)前執(zhí)行的中間件在chain中的位置 }, use: function(handle) { //這里默認(rèn) handle 是函數(shù),并且這里不做判斷 this.chain.push(handle); }, next: function() { //當(dāng)調(diào)用next時(shí)執(zhí)行index所指向的中間件 if (this.index >= this.chain.length) return; let middleware = this.chain[this.index]; this.index++; middleware(this.request, this.response, this.next.bind(this)); }, } function lineParser(req, res, next) { let items = req.requestLine.split(' '); req.methond = items[0]; req.url = items[1]; req.version = items[2]; next(); //執(zhí)行下一個(gè)中間件 } function headersParser(req, res, next) { let items = req.headers.split('\r\n'); let header = {} for(let i in items) { let item = items[i].split(':'); let key = item[0]; let value = item[1]; header[key] = value; } req.header = header; next(); //執(zhí)行下一個(gè)中間件 } function bodyParser(req, res, next) { let bodyStr = req.requestBody; let body = {}; let items = bodyStr.split('&'); for(let i in items) { let item = items[i].split('='); let key = item[0]; let value = item[1]; body[key] = value; } req.body = body; next(); //執(zhí)行下一個(gè)中間件 } function middleware3(req, res, next) { console.log('url: '+req.url); console.log('methond: '+req.methond); console.log('version: '+req.version); console.log(req.body); console.log(req.header); next(); //執(zhí)行下一個(gè)中間件 } let app = App(); app.use(lineParser); app.use(headersParser); app.use(bodyParser); app.use(middleware3); app.next();
運(yùn)行結(jié)果
將以上整體代碼運(yùn)行后將打印以下信息
url: /iven_ methond: POST version: HTTP/1.1 {key1: "value1", key2: "value2", key3: "value3"} {Host: "www.baidu.com", Cookie: "BAIDUID=E063E9B2690116090FE24E01ACDDF4AD"}
看完了這篇文章,相信你對(duì)“express實(shí)現(xiàn)中間件的原理分析”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!