中间件 在 Node.js 中被普遍使用,它泛指一种特定的设计模式、一系列的处理单元、过滤器和处理程序,以函数的形式存在,链接在一块儿,造成一个异步队列,来完成对任何数据的预处理和后处理。javascript
它的优势在于 灵活性:使用中间件咱们用极少的操做就能获得一个插件,用最简单的方法就能将新的过滤器和处理程序扩展到现有的系统上。java
中间件模式中,最基础的组成部分就是 中间件管理器,咱们能够用它来组织和执行中间件的函数,如图所示: git
要实现中间件模式,最重要的实现细节是:github
use()
函数来注册新的中间件,一般,新的中间件只能被添加到高压包带的末端,但不是严格要求这么作;至于怎么处理传递数据,目前没有严格的规则,通常有几种方式设计模式
而具体的处理方式取决于 中间件管理器 的实现方式以及中间件自己要完成的任务类型。api
举一个来自于 《Node.js 设计模式 第二版》 的一个为消息传递库实现 中间件管理器 的例子:app
class ZmqMiddlewareManager {
constructor(socket) {
this.socket = socket;
// 两个列表分别保存两类中间件函数:接受到的信息和发送的信息。
this.inboundMiddleware = [];
this.outboundMiddleware = [];
socket.on('message', message => {
this.executeMiddleware(this.inboundMiddleware, {
data: message
});
});
}
send(data) {
const message = { data };
this.excuteMiddleware(this.outboundMiddleware, message, () => {
this.socket.send(message.data);
});
}
use(middleware) {
if(middleware.inbound) {
this.inboundMiddleware.push(middleware.inbound);
}
if(middleware.outbound) {
this.outboundMiddleware.push(middleware.outbound);
}
}
exucuteMiddleware(middleware, arg, finish) {
function iterator(index) {
if(index === middleware.length) {
return finish && finish();
}
middleware[index].call(this, arg, err => {
if(err) {
return console.log('There was an error: ' + err.message);
}
iterator.call(this, ++index);
});
}
iterator.call(this, 0);
}
}
复制代码
接下来只须要建立中间件,分别在inbound
和outbound
中写入中间件函数,而后执行完毕调用next()
就行了。好比:框架
const zmqm = new ZmqMiddlewareManager();
zmqm.use({
inbound: function(message, next) {
console.log('input message: ', message.data);
next();
},
outbound: function(message, next) {
console.log('output message: ', message.data);
next();
}
});
复制代码
Express 所推广的 中间件 概念就与之相似,一个 Express 中间件通常是这样的:dom
function(req, res, next) { ... }
复制代码
前面展现的中间件模型使用回调函数实现的,可是如今有一个比较时髦的 Node.js 框架Koa2
的中间件实现方式与以前描述的有一些不太相同。Koa2
中的中间件模式移除了一开始使用ES2015
中的生成器实现的方法,兼容了回调函数、convert
后的生成器以及async
和await
。koa
在Koa2
官方文档中给出了一个关于中间件的 洋葱模型,以下图所示:
从图中咱们能够看到,先进入inbound
的中间件函数在outbound
中被放到了后面执行,那么到底是为何呢?带着这个问题咱们去读一下Koa2
的源码。
在koa/lib/applications.js
中,先看构造函数,其它的均可以无论,关键就是this.middleware
,它是一个inbound
队列:
constructor() {
super();
this.proxy = false;
this.middleware = [];
this.subdomainOffset = 2;
this.env = process.env.NODE_ENV || 'development';
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
}
复制代码
和上面同样,在Koa2
中也是用use()
来把中间件放入队列中:
use(fn) {
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
if (isGeneratorFunction(fn)) {
deprecate('Support for generators will be removed in v3. ' +
'See the documentation for examples of how to convert old middleware ' +
'https://github.com/koajs/koa/blob/master/docs/migration.md');
fn = convert(fn);
}
debug('use %s', fn._name || fn.name || '-');
this.middleware.push(fn);
return this;
}
复制代码
接着咱们看框架对端口监听进行了一个简单的封装:
// 封装以前 http.createServer(app.callback()).listen(...)
listen(...args) {
debug('listen');
const server = http.createServer(this.callback());
return server.listen(...args);
}
复制代码
中间件的管理关键就在于this.callback()
,看一下这个方法:
callback() {
const fn = compose(this.middleware);
if (!this.listenerCount('error')) this.on('error', this.onerror);
const handleRequest = (req, res) => {
const ctx = this.createContext(req, res);
return this.handleRequest(ctx, fn);
};
return handleRequest;
}
复制代码
这里的compose
方法其实是Koa2
的一个核心模块koa-compose
(https://github.com/koajs/compose),在这个模块中封装了中间件执行的方法:
function compose (middleware) {
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!')
for (const fn of middleware) {
if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')
}
/** * @param {Object} context * @return {Promise} * @api public */
return function (context, next) {
// last called middleware #
let index = -1
return dispatch(0)
function dispatch (i) {
if (i <= index) return Promise.reject(new Error('next() called multiple times'))
index = i
let fn = middleware[i]
if (i === middleware.length) fn = next
if (!fn) return Promise.resolve()
try {
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
} catch (err) {
return Promise.reject(err)
}
}
}
}
复制代码
能够看到,compose
经过递归对中间件队列进行了 反序遍历,生成了一个Promise
链,接下来,只须要调用Promise
就能够执行中间件函数了:
handleRequest(ctx, fnMiddleware) {
const res = ctx.res;
res.statusCode = 404;
const onerror = err => ctx.onerror(err);
const handleResponse = () => respond(ctx);
onFinished(res, onerror);
return fnMiddleware(ctx).then(handleResponse).catch(onerror);
}
复制代码
从源码中能够发现,next()
中返回的是一个Promise
,因此通用的中间件写法是:
app.use((ctx, next) => {
const start = new Date();
return next().then(() => {
const ms = new Date() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});
});
复制代码
固然若是要用async
和await
也行:
app.use((ctx, next) => {
const start = new Date();
await next();
const ms = new Date() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});
复制代码
因为还有不少Koa1
的项目中间件是基于生成器的,须要使用koa-convert
来进行平滑升级:
const convert = require('koa-convert');
app.use(convert(function *(next) {
const start = new Date();
yield next;
const ms = new Date() - start;
console.log(`${this.method} ${this.url} - ${ms}ms`);
}));
复制代码
最后,若是以为文章有点用处的话,求求大佬点个赞!若是发现什么错漏也欢迎提出!