从express源码分析express启动流程

    xiaoxiao2022-07-03  119

    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)