admin管理员组文章数量:1357393
I was reading the Socket.io Chat Demo here: / and I got confused when looking at their require statements.
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Am I correct in thinking that require("express")
produces an executable Express function (with all of the necessary functions and fields that go with that), and that require("http").Server(app)
creates a http.Server object with all of its fields and functions.
If so, I am confused because Express creates a server when we call the .listen function so it seems redundant and backwards to pass an Express app to an http module server.
So, my question is, what is really happening here?
I was reading the Socket.io Chat Demo here: http://socket.io/get-started/chat/ and I got confused when looking at their require statements.
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Am I correct in thinking that require("express")
produces an executable Express function (with all of the necessary functions and fields that go with that), and that require("http").Server(app)
creates a http.Server object with all of its fields and functions.
If so, I am confused because Express creates a server when we call the .listen function so it seems redundant and backwards to pass an Express app to an http module server.
So, my question is, what is really happening here?
Share Improve this question edited Dec 15, 2014 at 20:14 Alex Booth asked Dec 15, 2014 at 19:59 Alex BoothAlex Booth 731 silver badge6 bronze badges 3-
You're creating a webserver "instance" and passing it to sockec.io so it can use it for transport.
require
is just a function, if you return another function from the export, it can be called with arguments, and you can pass stuff to the middleware. – adeneo Commented Dec 15, 2014 at 20:01 -
ExpressJS'
app.listen()
is just for convenience for simple applications. It useshttp.Server(app)
, or ratherhttp.createServer(app);
, itself. – Jonathan Lonowski Commented Dec 15, 2014 at 20:36 -
so theoretically, I could just also write
io = require('socket.io')(http.createServer(app))
which should be the same then? – MarcL Commented Mar 9, 2021 at 10:33
3 Answers
Reset to default 4The http server expects a function which has the following signature:
function(req, res)
require('express')();
will create a function with that signature which handles all the magic that express makes available like routing, middleware, etc. Express can create it's own http server instance, but since you're also using socket.io (which expects access to the http server as well), you'll need a separate http instance.
var app = require('express')(); //infact, app is a requestListener function.
var http = require('http').Server(app); // infact, function Server eqs http.createServer;
// 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./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./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./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./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);
});
const app = express();
let server = require('http').Server(app);
server.listen(3000);
This makes the server listening to the port we provide.
const app = express();
let server = require('http').createServer(app);
server.listen(3000);
So Server and createServer are the same according to the node.js HTTP library source code.
Ref : nodejs/lib/http.js
exports.Server = Server;
exports.createServer = function(requestListener) {
return new Server(requestListener);
};
本文标签:
版权声明:本文标题:javascript - What is happening when require("http").Server() is evaluated with an Express app as its argument? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744059404a2583849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论