What are the benefits of using http with express

advertisements

I've been using the Node.js express module for some time now, without ever using the http module to listen to the port.

I'm wondering what are the benefits of using:

app = express();
app.listen(app.get('port'));

over

app = express();
var server = http.createServer(app).listen(app.get('port'));

My guess is that it's something to do with being able to set http settings such as maxSockets etc, but is there any other reason people do this?


From http://expressjs.com/api.html#app.listen:

The app.listen() method is simply a convenience method.

Here's the listen definition:

app.listen = function(){
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

Notice it passes its arguments to the server's listen call, so you can still sett http settings like maxSockets.

It also says, "if you wish to use HTTPS or provide both, use the technique above." The technique above it refers to is:

http.createServer(app).listen(80);
https.createServer(options, app).listen(443);

So it seems one of the most common reasons not to use app.listen is if you want to have an https server.

I suppose you might need a reference to the return value of http.createServer for some reason, in which case you wouldn't want to use app.listen.