Express.js - app.listen vs server.listen


node.js Problem Overview

This may be a very basic question but I simply don't get it. What is the difference between creating an app using Express.js and starting the app listening on port 1234, for example:

var express = require('express');
var app = express();

//app.configure, app.use etc


and adding an http server:

var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

//app.configure, app.use etc


What's the difference?
If I navigate to http://localhost:1234, thus I get the same output.

node.js Solutions

Solution 1 - node.js

The second form (creating an HTTP server yourself, instead of having Express create one for you) is useful if you want to reuse the HTTP server, for example to run within the same HTTP server instance:

var express = require('express');
var app     = express();
var server  = require('http').createServer(app);
var io      = require('').listen(server);

However, app.listen() also returns the HTTP server instance, so with a bit of rewriting you can achieve something similar without creating an HTTP server yourself:

var express   = require('express');
var app       = express();

// app.use/routes/etc...

var server    = app.listen(3033);
var io        = require('').listen(server);

io.sockets.on('connection', function (socket) {

Solution 2 - node.js

There is one more difference of using the app and listening to http server is when you want to setup for https server

To setup for https, you need the code below:

var https = require('https');
var server = https.createServer(app).listen(config.port, function() {
    console.log('Https App started');

The app from express will return http server only, you cannot set it in express, so you will need to use the https server command

var express = require('express');
var app = express();

Solution 3 - node.js

Just for punctuality purpose and extend a bit Tim answer.
From official documentation:

> The app returned by express() is in fact a JavaScript Function, > DESIGNED TO BE PASSED to Node’s HTTP servers as a callback to handle > requests.

> This makes it easy to provide both HTTP and HTTPS versions of your > app with the same code base, as the app does not inherit from these > (it is simply a callback):

var https =require('https');
var http = require('http');
https.createServer(options, app).listen(443);

> The app.listen() method returns an http.Server object and (for HTTP) > is a convenience method for the following:

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

Solution 4 - node.js

I came with same question but after google, I found there is no big difference :)

From Github

If you wish to create both an HTTP and HTTPS server you may do so with the "http" and "https" modules as shown here.

 * Listen for connections.
 * A node `http.Server` is returned, with this
 * application (which is a `Function`) as its
 * callback. If you wish to create both an HTTP
 * and HTTPS server you may do so with the "http"
 * and "https" modules as shown here:
 *    var http = require('http')
 *      , https = require('https')
 *      , express = require('express')
 *      , app = express();
 *    http.createServer(app).listen(80);
 *    https.createServer({ ... }, app).listen(443);
 * @return {http.Server}
 * @api public

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

Also if you want to work with see their example

See this

I prefer app.listen() :)

Solution 5 - node.js

Express is basically a wrapper of http module that is created for the ease of the developers in such a way that..

  1. They can set up middlewares to respond to HTTP Requests (easily) using express.
  2. They can dynamically render HTML Pages based on passing arguments to templates using express.
  3. They can also define routing easily using express.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTamasView Question on Stackoverflow
Solution 1 - node.jsrobertklepView Answer on Stackoverflow
Solution 2 - node.jsTimView Answer on Stackoverflow
Solution 3 - node.jsIvan TalalaevView Answer on Stackoverflow
Solution 4 - node.jsMuhammad ShahzadView Answer on Stackoverflow
Solution 5 - node.jsSarim Javaid KhanView Answer on Stackoverflow