Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http.listen vs app.listen #19

Open
nickrttn opened this issue Dec 16, 2015 · 1 comment
Open

http.listen vs app.listen #19

nickrttn opened this issue Dec 16, 2015 · 1 comment

Comments

@nickrttn
Copy link

In the index.js the app object is being passed into http.Server(), which is later on called with http.listen(). Is there any difference or upside to this method over just using express' builtin app.listen();? Why and how does this work?

@jsmoorman
Copy link

You must pass an HTTP server instance to socket which is created by the line:

var http = require('http').Server(app);

Express' built in listen() function does return an HTTP server instance, so it is possible to use app.listen().

This:

var app = require('express')();
var http = require('http').Server(app); // The server instance is created here.
var io = require('socket.io')(http); // The server instance is passed here.

Is equivalent to:

var app = require('express')();
var server = app.listen(3033); // The server instance is returned from app.listen()
var io = require('socket.io').listen(server); // The server instance is passed here.

However, I'm unsure if either provide any benefits over the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants