app = require('express')();
var http = require('http').Server(app);
// infact,app.listen eqs http.listen
other code from nodejs and express model
we can see, require("express")() return app is a function.
//express/lib/express.js https://github.com/strongloop/express/blob/master/lib/express.js
var proto = require('./application');
exports = module.exports = createApplication;
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;
}
we can see, node.createServer eqs Server
//nodejs/lib/http.js https://github.com/nodejs/node/blob/master/lib/http.js
exports.createServer = function(requestListener) {
return new Server(requestListener);
we can see, express app.listen eqs http.listen
//express/lib/application.js https://github.com/strongloop/express/blob/master/lib/application.js
app.listen = function listen() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
//nodejs/lib/_http_server.js https://github.com/nodejs/node/blob/master/lib/_http_server.js
function Server(requestListener) {
if (!(this instanceof Server)) return new Server(requestListener);
net.Server.call(this, { allowHalfOpen: true });
if (requestListener) {
this.addListener('request', requestListener);
}
this.httpAllowHalfOpen = false;
this.addListener('connection', connectionListener);
this.addListener('clientError', function(err, conn) {
conn.destroy(err);
});