Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Latest commit

 

History

History
64 lines (47 loc) · 1.4 KB

express.md

File metadata and controls

64 lines (47 loc) · 1.4 KB

express.js

1. createApplication

在使用express的时候,首先是:

var express = require('express');
var app = express();

在源码中,有这样一句:

exports = module.exports = createApplication;

因此,express.js的导出对象实际上就是createApplication这个方法,该方法定义如下:

/**
 * Create an express application.
 *
 * @return {Function}
 * @api public
 */

function createApplication() {
  var app = function(req, res, next) {
    app.handle(req, res, next);
  };

  mixin(app, EventEmitter.prototype, false);
  mixin(app, proto, false);

  app.request = { __proto__: req, app: app };
  app.response = { __proto__: res, app: app };
  app.init();
  return app;
}

可以看到,该方法实际上就是一个工厂函数,用来生成一个app的实例。而app事实上是一个函数,该函数会有一些其它属性。

其中有两句:

mixin(app, EventEmitter.prototype, false);
mixin(app, proto, false);

mixin是一个依赖模块,它其实就是一个extend操作,这两行代码就是吧EventEmitter.prototypeproto的属性扩展到app上。

2. module.exports

下面的代码就是为导出对象添加一些属性,综合起来,module.exports,即express,有如下属性:

  • application
  • request
  • response
  • Route
  • Router
  • query
  • static