菜单

js_脚本之家

2020年3月23日 - 新闻中心

简介

中间件机制得以让大家在多个加以的流水生产线中加多叁个甩卖步骤,进而对那个流程的输入大概输出发生影响,只怕产生部分中功用、状态,大概阻止这么些流程。中间件机制和tomcat的过滤器近似,那四头都归于权利链方式的实际完结。

express 中间件使用案例

let express = requirelet app = express()//解析request 的bodyapp.use//解析 cookieapp.use//拦截app.get('/hello', function  { res.send;

仿照中间件机制而且模拟完成分析request的中间件

js_脚本之家。率先进轨范拟三个request

request = { //模拟的request requestLine: 'POST /iven_ HTTP/1.1', headers: 'Host:www.baidu.comrnCookie:BAIDUID=E063E9B2690116090FE24E01ACDDF4AD:FG=1;BD_HOME=0', requestBody: 'key1=value1&key2=value2&key3=value3',}

一个http伸手分为诉求行、央浼头、和诉求体,那三者之间通过rnrn即二个空行来划分,这里假若已经将那三者分开,requestLine中有法子类型,央浼url,http版本号,那三者通过空格来差异,headers中的各部分通过rn来分割,requestBody中通过
& 来分别参数

依傍中间件机制

约定 中间件一定是一个函数而且选取 request, response, next多少个参数

function App() { if (! return new App;}App.prototype = { constructor: App, init: function() { this.request = { //模拟的request requestLine: 'POST /iven_ HTTP/1.1', headers: 'Host:www.baidu.comrnCookie: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 是函数,并且这里不做判断 this.chain.push; }, 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; },}

对 request 管理的中间件

 function lineParser { let items = req.requestLine.split; req.methond = items[0]; req.url = items[1]; req.version = items[2]; next(); //执行下一个中间件 }function headersParser { let items = req.headers.split; let header = {} for { let item = items[i].split; let key = item[0]; let value = item[1]; header[key] = value; } req.header = header; next(); //执行下一个中间件}function bodyParser { let bodyStr = req.requestBody; let body = {}; let items = bodyStr.split; for { let item = items[i].split; let key = item[0]; let value = item[1]; body[key] = value; } req.body = body; next(); //执行下一个中间件}function middleware3 { console.log; console.log('methond: '+req.methond); console.log('version: '+req.version); console.log; console.log; //执行下一个中间件}

let app = App;app.use;app.use;app.use;app.next();

全体代码

function App() { if (! return new App;}App.prototype = { constructor: App, init: function() { this.request = { //模拟的request requestLine: 'POST /iven_ HTTP/1.1', headers: 'Host:www.baidu.comrnCookie: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 是函数,并且这里不做判断 this.chain.push; }, 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; },}function lineParser { let items = req.requestLine.split; req.methond = items[0]; req.url = items[1]; req.version = items[2]; next(); //执行下一个中间件 }function headersParser { let items = req.headers.split; let header = {} for { let item = items[i].split; let key = item[0]; let value = item[1]; header[key] = value; } req.header = header; next(); //执行下一个中间件}function bodyParser { let bodyStr = req.requestBody; let body = {}; let items = bodyStr.split; for { let item = items[i].split; let key = item[0]; let value = item[1]; body[key] = value; } req.body = body; next(); //执行下一个中间件}function middleware3 { console.log; console.log('methond: '+req.methond); console.log('version: '+req.version); console.log; console.log; //执行下一个中间件}let app = App;app.use;app.use;app.use;app.next();

运维结果

将以上全体代码运维后将打印以下新闻

url: /iven_methond: POSTversion: HTTP/1.1{key1: "value1", key2: "value2", key3: "value3"}{Host: "www.baidu.com", Cookie: "BAIDUID=E063E9B2690116090FE24E01ACDDF4AD"}

如上便是本文的全体内容,希望对大家的学习抱有助于,也可望大家多多点拨脚本之家。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图