从express源码分析express启动流程

1)加载express模块

var express = require("express"); 
2)express导出的是一个函数,因此 express()这样写就是调用一个函数

module.exports = require('./lib/express');
 
exports = module.exports = createApplication; 
 
也就是说:express()是调用一个函数
3)app通过mixin继承了application的方法

exports = module.exports = createApplication;
 
var proto = require('./application');
 
function createApplication() {
  var app = function(req, res, next) {
    app.handle(req, res, next);
  };
 
  mixin(app, EventEmitter.prototype, false);
  mixin(app, proto, false); // 主要是这行
 
  // expose the prototype that will get set on requests
  app.request = Object.create(req, {
    app: { configurable: true, enumerable: true, writable: true, value: app }
  })
 
  // expose the prototype that will get set on responses
  app.response = Object.create(res, {
    app: { configurable: true, enumerable: true, writable: true, value: app }
  })
 
  app.init();
  return app;
}
4)application.js 封装了http模块

app.listen = function listen() {
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};
总体来说:express没有增加功能,只是封装了http模块
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值