Sunday, December 30, 2018

Getting start with Node HTTP processing - step by step

Episode 1 - Creating a web server with node

Why this series?

I start his series as a way to keep my notes on how I learn nodejs. If you have a comment or a suggesting, please drop me a note.

Prerequisite

You should familiar with the following topics:

  • Computer programming. Experience with any programming language will do. I recommend Coursera if you are looking for brushing up or learning a language.
  • JavaScript - Knowing JavaScript programming is definitely good. But it is not a deal breaker. I didn't know anything about JavaScript when I started this. I google most of the stuff as I go along.
  • Access to a computer. You should definitely have access to a working computer. Any operating system will do. "Node" runs on most of them. You should definitely make sure node install on it. Looking at https://nodejs.org for an instruction on how to install it on your computer.
  • You know how to create a text file and save on the computer

The very first example in the "Getting Start" section of the nodejs is to create a simple web server. The code is very similar to what I have here in Figure 1. In this example, the node creates an HTTP server. The server listens to the incoming HTTP request to the IP address 127.0.0.1 port 3333. "127.0.0.1" is an internal address of a computer. Most, if not all, computers have one. I also call it a "loopback" address - a lot of people too. "3333" is the TCP port number. You can pick any number that is greater than 1024. We talk more about that later.

Figure 1. A simple "Hello World!" web server
// Most simple web server based on node
const http = require('http');

const srv = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/text'});
  res.end("Hello World!");
});

const myAddress = "127.0.0.1";
const myPort = 3333;

srv.listen(myPort, myAddress, () => {
  console.log("Listening on " + myPort);
});

Understand line-by-line

Let's go through each statement and see what they do.

  • The very first line on the top:
    // Most simple web server based on node
    
    This line begines with "//". This means that it is a comment. A comment will not be executed by "node". I use it for readability and as a note to people who will be reading my program.
  • The second line is
    const http = require('http');
    This tells node that I want to use a module call "http". "node" will load the module and store it in a constant variable name http. In node, a module is used to manage code. Instead of writing your own HTTP server or include other HTTP server code into your program, you can simply load a pre-built module and use it.

    There are several advantages of using a module insteads of writing your own code. One is saving your time. using a The "http" module is part of the node distribution. You don't need to install any module in order to use it.

  • The next part is a little bit longer. But it is really simple. Basically, this section define a "srv" constant based on http.createServer( ) method.
    const srv = http.createServer((req, res) => {
      res.writeHead(200, { 'Content-Type': 'text/text'});
      res.end("Hello World!");
    });
    
    We define the srv such that when a new srv is created by a http connection it send out a text message "Hello World!" with an appropriate HTTP header.
  • In previous steps, we prepare our code. We define things that we want our program to do, but we haven't create anything,yet. In the last step. We instruct the srv to start listening on TCP port 3333.
    const myAddress = "127.0.0.1";
    const myPort = 3333;
    
    srv.listen(myPort, myAddress, () => {
      console.log("Listening on 3333");
    
    The first to line define two constants that we will be using in srv.listen. These two constants are not necessary. We can actually put these two values inside the srv.listen() - like so srv.listen(3333, "127.0.0.1", () => { .... However, I have a habit of not using a constant ina system call. You can do it either way.
After the last step, you have yourself a mini web server listing to an incoming HTTP request on port 3333. To test this, you can use your favorite browser, or a program called curl to make a request to it as follow:
curl http://127.0.0.1:3333

How you enjoy reading this. I will update this from time to time. Please stop by and make a suggestion when you do.

References

  • "https://nodejs.org" - nodejs site - retrived 2018-12-29