const server = http.createServer(app);
const hostname = 'localhost';
const port = 3000;
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

The introduction of nodejs changed how the world, especially software developers, looked at websites and the technologies used to build them. Nodejs brought JavaScript from browser to desktop and servers. Though there were few methods to run server-side JavaScript, nodejs was a gamechanger. Many people came up with multiple frameworks and libraries based on nodejs to develop front-end like Angular and React. Multiple tools like npm and Grunt were introduced to meet different requirements like managing node packages.

Express JS was introduced as a framework to create web servers easily with nodejs. Express is minimal, flexible, and robust. HTTP utility methods and middleware included made the creation of APIs easy and quick. Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love. There exist many frameworks that is built using Express.

The command 

$ npm install express --save

will install the latest stable express on the system. 

First we need to import express using 

const express = require('express'),

Then we need to create an express app as follows

const app = express();

Let us create a simple web request handler as

app.use((req, res, next) => {
  console.log(req.headers);
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<html><body><h1>This is an Express Server</h1></body></html>');
});


then let us create a webserver and listen to it  at port 3000 of localhost

const server = http.createServer(app);
const hostname = 'localhost';
const port = 3000;
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Now start the server and go to the browser and type url http://localhost:3000/ You will see the message

This is an Express Server

Putting all together,


const express = require('express'),
     http = require('http');

const hostname = 'localhost';
const port = 3000;

const app = express();

app.use((req, res, next) => {
  console.log(req.headers);
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<html><body><h1>This is an Express Server</h1></body></html>');

});

const server = http.createServer(app);

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});