Your First Server/Application

In this section we shall make our first server. The World Wide Web is a giant client/user application. In order to visualize what that the web is thus placing the frontend, the backend and the net on a visual map, we shall take a look at two diagrams:

Figure 15.1. The World Wide Web on the Internet. High Level Perspective
The World Wide Web on the Internet. High Level Perspective

Figure 15.2. The Client/Server Communication. Lower Level Abstraction
The Client/Server Communication. Lower Level Abstraction

Before we make our first test application we use npm init to create the configuration package.json interactively. Alter the suggested index.js starting point to main.js. Put in some remarks such as "First Test Application" into the description. Finalizing that the command will present the file content for your approval.

Example 15.13. Initial packakge.json
{
  "name": "myg4",
  "version": "1.0.0",
  "description": "First Test Application",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Niels Müller Larsen <nmla@iba.dk> (http://x15.dk)",
  "license": "MIT"
}

Knowing one of the dependencies of the server/application we will build, let us see how to do that and get the configuration file updated in the process. On the command line we write

npm install http-status-codes

or the shorter version

npm i http-status-codes

This changes the configuration

Example 15.14. Changed Configuration package.json
{
  "name": "myg4",
  "version": "1.0.0",
  "description": "First Test Application",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Niels Müller Larsen <nmla@iba.dk> (http://x15.dk)",
  "license": "MIT",
  "dependencies": {
    "http-status-codes": "^1.4.0"
  }
}

We will notice the added dependencies object in the configuration.

You already saw a sneak preview of a server in so many lines of code. Now we will do it again. This time with the necessary explanatory remarks. Create an application directory, step into it and write:

Example 15.15. Your First Server, myg4/main.js
"use strict";

const http = require("http");
const httpStatus = require("http-status-codes");
const hostname = "127.0.0.1";
const port = 3000;
const app = http.createServer(function (req, res) { // server obj with call back
    console.log(`Received an incoming request from: ${req.url}`);
    console.log(`with HTTP-method: ${req.method}`);
    res.writeHead(httpStatus.OK, {
        "Content-Type": "text/html; charset=utf-8"
    });
    let responseMsg = "<h1>Hello Universe!</h1>";   // response text
    responseMsg += "<p><kbd>myg4</kbd> at your disposal</p>";
    res.write(responseMsg);                         // response body with text
    res.end();                                      // send http package
    console.log(`My response was: ${responseMsg}`)
});

app.listen(port, hostname, function () {
    console.log(`Server running, and listening at http://${hostname}:${port}/`);
});

On your CLI do node main. Then go to your browser and do http://localhost:3000.