2

I am learning express and I have seen 2 different ways of creating a server. Just curious on what's the difference between the 2 methods. Here's an express server done as in most tutorials:

var express = require('express');
var app = express();
app.listen(3000, function () {
    console.log('Example app listening on port 3000.');
});

and the second way I found, using http server.

var express = require('express');
var app = express();
var http = require('http');
var httpServer = http.createServer(app);
httpServer.listen(3000);

Why and when would I use one over the other? Does it make a big difference?
Thank you

Sivvio
  • 297
  • 2
  • 7
  • 22
  • 2
    Possible duplicate of [Express.js - app.listen vs server.listen](https://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen) – Kevin B Dec 18 '17 at 17:16

1 Answers1

3

The reason for the difference is that sometimes you want to create multiple Express instances for the purpose of routing and that only one of them should be listening. In the most trivial case, though, there's no tangible difference.

It's important to keep in mind that Express and the HTTP server are two entirely different things. The trick is that app.listen instantiates a server for you automatically, otherwise you have to do that yourself.

tadman
  • 208,517
  • 23
  • 234
  • 262