本篇主要介紹了淺談express 中間件機制及實現原理,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
簡介
中間件機制可以讓我們在一個給定的流程中添加一個處理步驟,從而對這個流程的輸入或輸出會產生影響,或是產生一些中作用、狀態,或是攔截這個流程。中間件機制和tomcat的過濾器類似,這兩者都屬於責任鏈模式的具體實現。
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!'); });
模擬中間件機制並且模擬實作解析request的中間件
先模擬一個request
request = { //模拟的request requestLine: 'POST /iven_ HTTP/1.1', headers: 'Host:m.sbmmt.com\r\nCookie:BAIDUID=E063E9B2690116090FE24E01ACDDF4AD:FG=1;BD_HOME=0', requestBody: 'key1=value1&key2=value2&key3=value3', }
一個http
請求分為請求行、請求頭、和請求體,這三者之間透過\r\n\r\n
即一個空白行來分割,這裡假設已經將這三者分開,requestLine
(請求行)中有方法類型,請求url,http版本號,這三者透過空格來區分,headers
(請求頭)中的各部分透過\r\n
來分割,requestBody
(請求體)中透過& 來區分參數
模擬中間件機制
約定中間件一定是一個函數並且接受request, response, 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:m.sbmmt.com\r\nCookie:BAIDUID=E063E9B2690116090FE24E01ACDDF4AD:FG=1;BD_HOME=0', requestBody: 'key1=value1&key2=value2&key3=value3', }; this.response = {}; //模拟的response this.chain = []; //存放中间件的一个数组 this.index = 0; //当前执行的中间件在chain中的位置 }, use: function(handle) { //这里默认 handle 是函数,并且这里不做判断 this.chain.push(handle); }, next: function() { //当调用next时执行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)); }, }
對request 處理的中間件
function lineParser(req, res, next) { let items = req.requestLine.split(' '); req.methond = items[0]; req.url = items[1]; req.version = items[2]; next(); //执行下一个中间件 } 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(); //执行下一个中间件 } 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(); //执行下一个中间件 } 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(); //执行下一个中间件 }
測試程式碼
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:m.sbmmt.com\r\nCookie:BAIDUID=E063E9B2690116090FE24E01ACDDF4AD:FG=1;BD_HOME=0', requestBody: 'key1=value1&key2=value2&key3=value3', }; this.response = {}; //模拟的response this.chain = []; //存放中间件的一个数组 this.index = 0; //当前执行的中间件在chain中的位置 }, use: function(handle) { //这里默认 handle 是函数,并且这里不做判断 this.chain.push(handle); }, next: function() { //当调用next时执行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(); //执行下一个中间件 } 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(); //执行下一个中间件 } 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(); //执行下一个中间件 } 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(); //执行下一个中间件 } let app = App(); app.use(lineParser); app.use(headersParser); app.use(bodyParser); app.use(middleware3); app.next();
url: /iven_ methond: POST version: HTTP/1.1 {key1: "value1", key2: "value2", key3: "value3"} {Host: "m.sbmmt.com", Cookie: "BAIDUID=E063E9B2690116090FE24E01ACDDF4AD"}
以上是解析express中介軟體機制及其實作原理的詳細內容。更多資訊請關注PHP中文網其他相關文章!