Here are two examples of creating Node.js servers with 5 (or 6) lines of code. There is no routing being done (GET, POST, etc.) and all our server does is return ‘Hello World!’ in the HTTP response body.

var http = require('http');
var server = http.createServer(function(req, res) {
 res.end('Hello World!');
});
server.listen(8080, '127.0.0.1');

// Require Node's http module
// Creates the server with a req/res handler
// Writes 'Hello World' to the response body and sends the response
// Our server will be listening to requests made to localhost on port 8080

In this first example, we are using Node’s built-in ‘HTTP’ module. We create the server instance with the method, ‘createServer’, which takes a callback function invoked with request and response objects. To actually have the server listening to requests, we use the method ‘listen’ with our specified port and IP address.

var express = require('express');
var app = express();
app.use(function(request, response) {
 res.end('Hello World!');
});
app.listen(8080);

In this second example, we are using the ‘express’ module which is a wrapper around the HTTP module used above. Express abstracts some of the boilerplate code and provides a more accessible interface with your Node server. Here, we use 6 lines instead of 5 :(.

Comparing both examples, the code here looks almost the same as the previous which makes it hard to see the apparent benefits of using express vs. http (bare node). Some express methods such as ‘.get’ and ‘.post’ make it easy to handle the respective HTTP request methods and middleware allows for easier parsing, authentication, and session handling, to name a few pros.

References

https://nodejs.org/api/http.html

http://expressjs.com/en/starter/hello-world.html