简介
中间件机制可以让我们在一个给定的流程中添加一个处理步骤,从而对这个流程的输入或者输出产生影响,或者产生一些中作用、状态,或者拦截这个流程。中间件机制和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: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 (!(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.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) { //这里默认 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));









