koa源码分析
一、Koa介绍
Koa 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小、更富有表现力、更健壮的基石。 通过利用 async 函数丢弃回调函数,并有力地增强错误处理。 Koa 并没有捆绑任何中间件, 而是提供了一套优雅的方法,帮助开发者快速而愉快地编写服务端应用程序。
相对使用koa本身,koa更重要的是成为了一个基石。由于koa对开发者的束缚更小,让开发者可以基于koa去完成功能更丰富的框架,比如gulu和egg就是基于koa的二次封装。
二、Koa源码分析
Koa主要包含4个js文件,包括application.js,context.js,request.js, response.js。
- application:koa应用实例。
- context:一次请求的上下文环境。
- request:基于node原生req对象封装后的koa的request对象。
- response:基于node原生res对象封装后的koa的response对象。
流程:
1. application.js
- 构造函数,暴露了Application类,继承了Emitter模块。构造函数做了以下几件事。
- 默认不设置代理
- 初始化中间件数组middleware
- 子域名偏移量默认为2,也就是默认忽略数量为2
- 假设域名是"tobi.ferrets.example.com"。如果app.subdomainOffset没有设置,也就是说默认要忽略的偏移量是2,那么ctx.subdomains是[“ferrets”, “tobi”]。
- 环境变量的处理
- 挂载context、request、response
- 重写util.inpsect方法
- util.inspect是一个将任意对象转换为字符串的方法,通常用于调试和错误输出。在node 6+版本中,util.inspect.custom返回一个Symbol类型。对象用util.inspect.custom作为key,值为函数的话,在对对象使用util.inspect()时候,util.inpsect会被该函数覆盖。
module.exports = class Application extends Emitter {
/**
* Initialize a new `Application`.
*
* @api public
*/
constructor(options) {
super();
options = options || {
};
this.proxy = options.proxy || false;
this.subdomainOffset = options.subdomainOffset || 2;
this.proxyIpHeader = options.proxyIpHeader || 'X-Forwarded-For';
this.maxIpsCount = options.maxIpsCount || 0;
this.env = options.env || process.env.NODE_ENV || 'development';
if (options.keys) this.keys = options.keys;
this.middleware = [];
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
// util.inspect.custom support for node 6+
/* istanbul ignore else */
if (util.inspect.custom) {
this[util.inspect.custom] = this.inspect;
}
}
- only方法返回对象白名单属性,即返回只想对外暴露的属性。此处表明只对外暴露‘subdomaimOffset’、‘proxy’以及‘env’三个属性。
/**
* Return JSON representation.
* We only bother showing settings.
*
* @return {Object}
* @api public
*/
toJSON() {
return only(this, [
'subdomainOffset',
'proxy',
'env'
]);
}
- 这时候再利用util.inspect将app实例转换为字符串的话,只能看到经过only处理后暴露的属性。
/**
* Inspect implementation.
*
* @return {Object}
* @api public
*/
inspect() {
return this.toJSON();
}
- 服务器的启动,调用Node.js的http模块来创建一个服务器,具体的handler是使用callback方法的返回值。通过(…args)将listen的参数转发给创建好的server。
/**
* Shorthand for:
*
* http.createServer(app.callback()).listen(...)
*
* @param {Mixed} ...
* @return {Server}
* @api public
*/
listen(...args) {
debug('listen');
const server = http.createServer(this.callback());
return server.listen(...args);
}
}
- this.callback()返回一个handleRequest函数处理请求
- callback函数首先利用compose组合已注册的中间件,形成函数fn
- 利用了闭包的性质,将fn持久化,不用每次接收到请求后都重新对中间件进行组合
- 此时每次到达一个新的请求,都会执行一次handleRequest
- 该函数首先会结合node的生的req和res对象创建一个请求上下文ctx
- 然后将ctx和fn一起传入this.handleRequest函数进行处理,返回结果
/**
* Return a request handler callback
* for node's native http server.
*
* @return {Function}
* @api public
*/
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;
}
- 该函数进行了以下操作,这些操作的目的是为了让开发者更加自由的去访问某一个对象
- 首先创建了一个context对象
- 然后基于node原生的req和res创建koa实例的request和response对象,并挂载到context上
- 将koa实例分别挂载到context、request、response上
- 将node原生req分别挂载到context、request、response上
- 将node原生res分别挂载到context、request、response上
- 将context挂载到request、response上
- 将resquest和response分别互相挂载
- 将req.url挂载到context和request上
/**
* Initialize a new context.
*
* @api private
*/
createContext(req, res) {
const context = Object.create(this.context);
const request = context.request = Object.create(this.request);
const response = context.response = Object.create(this.response);
context.app = request.app = response.app = this;
context.req = r