Samz
Samz

Reputation: 86

NodeJS express - What is the difference and correct way of creating a basic server

What is the difference and what is the most correct and latest way when creating a server with nodejs express?

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

app.listen(process.env.PORT || 3000, function () {
console.log('server running')
});

and the second way

const express = require('express');
const app = express();
const server = require('http').createServer(app)

server.listen(process.env.PORT || 3000, function () {
console.log('server running')
});

Upvotes: 0

Views: 40

Answers (1)

James
James

Reputation: 82136

In terms of correctness, there is no difference, express basically does what you're doing in the second example when app.listen is called.

Check out the source

Upvotes: 2

Related Questions