Realtime and Scalable Web Applications

How to write a basic computer program: Learn with node.js

Wordpress Code Backgrounds

We will take the example of the http server which we created for the hello world application.

const http = require('http')

http.createServer((req, resp) => {

resp.writeHead(200, {
        'Content-Type': 'text/plain'
resp.write('Hello, World!\n')



Note: according to the naming conventions we have named the request and response objects as “req” and “resp”. Now we can have put any other word whatever we have wanted to write like we can have simply wrote request and response, or just r1, r2. Again one thing to note here is that whatever name we have already defined like at first we have defined r1 or req, we cannot use the same name again as this would result in a duplicate variable error. 

We will start with the 1st line and the explanation of this line goes as below.

A program logic is comprised of a specific group of words which are described as a functions, 

reserved keywords,

Variables/data types, 




Abstraction and encapsulation(also known as scope of a specific set of instructions.), comments,


Calling of objects,

Asynchronous callbacks and a few other things.

A typical programming logic will contain a mixture of all of these kinds of instructions.

These instructions are human understandable which means a human being can easily comprehend these set of instructions and develop a logic for the program rather than getting into the details of the hardware/low level language which becomes very difficult and lengthy to understand and comprehend.

These human/high level language is then compiled by the compiler which means this high level language is converted to the machine level language in order to make the machine understand that exactly what we are trying to write as a program.

It is then further processed by the interpreter which lets the machine interpret what actually we are writing as an instruction for the machine to perform.

We now start at line 1.

As any other programming language, Node.js has its own keywords. These are special words that have a specific meaning in the Node.js context and that you can’t use as identifiers (variables, functions, etc.) in your code. In this article, we’ll take a look at the most important ones. 

Keywords are an important part of any programming language. They help to identify the purpose and function of code, making it easier to understand and work with. In Node.js, there are a few specific keywords that are worth knowing about.

Every programming language has certain words reserved for internal use and that have some meaning for the language. These words are referred to as Reserved Keywords or termed as Keywords.

Therefore, the words or Keywords that any programming language uses internally, for a specific purpose, are not allowed to be used for anything other than that purpose. If the language compiler does this, the language will be confused.

The word “const” is a keyword for node.js. const is used to define and hold a variable, a variable is used to store some value of the defined data type. 

In this case we are defining a variable named as “http”, and a variable is a data type and the type of data it is holding is one of the core module in node.js. “http” is a core module in node.js which will help us import the http module in order to create a http web server.

A const data type will not allow the data it is holding to change its value or reassign any other new value. So in this case the const data type will only hold a “http” module data type and this data type is not allowed to change its value in the whole program which is written after this.

Now in order to make the value change itself we will define a let keyword data type. It will be then written as this-

explanation of program code

let http = require(‘http’)

Now in this case the http module value can be changed or reassigned with a new data type. Now if we want to assign a value for example to hold a integer value or a number lets say the number 80 then we can reassign the “http” variable like this

http = 80

If we want to reassign further with a sentence “ this is programming in node.js”

Then we do this –

http = “ this is programming in node.js”

Now we can keep reassigning values to this data type but in case if it was declared as a const variable then this reassignment of other data types won’t be possible.

Another concept we have to know is how we declare and define a variable or a function.

const http = require(‘http’)

In this case we can say that we have declared a variable named http of a “const” data type and defined this variable as requiring the http module.

“Require” is another keyword in node.js which tells the program that we need to require or import a certain module which is either a core module or a local or a third party module. 

Please refer to this page for more information about modules.

Now moving on to line 2, here we are creating a web server instance of a data type of http which we had declared in line 1.

explanation of programming logic

2.1 We are using the const data type of http to execute a function which will create a web server.

2.2 Now we are using the function createServer after specifying a dot in the http data type.

Since the data type is of core module http we are able to create a predefined function named “createserver”. Note that creation of this server is only possible with this particular data type which is “http”.

This will not be possible if the data type is of another type like in the case earlier where we reassigned this data type with a number, and a sentence. 

Since http is a type of core module, the function “createserver” only belongs to the http module.

2.3 the curly braces define the beginning of the set of instructions to be executed inside the createServer function.

2.4 a subset of curly braces containing the data type of req and res(request and response).

These two values/parameters hold the data associated with the incoming request and also the data associated with the type of response we are about to write for this http request.

Read more about http request and response here

For example if there is a certain data passed by the client in the request parameter like a request body or a request header, those can be accessed from the variable “req”

And the response we want to send for this particular request there are certain data associated with it which can be accessed by the “res” parameter.

2.5 an equal to sign followed by a > signifies

that the createserver function is of type asynchronous which means it is non blocking. Read more about non-blocking code here

 If a program has this sign immediately after the declaration of the function this specifies that this function is an asynchronous function, which is the main idea behind node.js programming.

2.6 another layer of abstraction which denotes the start of the code of the createserver

function, containing the set of instructions to be executed once a http web server is created successfully.

The code inside this is 

resp.writeHead(200, {

        ‘Content-Type’: ‘text/plain’


resp.write(‘Hello, World!\n’)



So after successful creation of the http web server this is the set of instructions that are need to be performed once the web server has been created.

Then we have sent a response to that particular request.

We have first written the response headers with a 200 HTTP status code meaning the request has been processed successfully.

We have then defined the content type which is a plan text type of content. This content is what is the response sent to the http incoming request.

So inside a web browser when we will call this API we will see a response written as “hello World” and this response will be in plain text format.

Then we have put an end to the response by specifying resp.end()

Another thing we would like to take a note here is we have called resp.end and after that specified a () curly braces. We do so specifically when we have to call an object of a particular function.

In this case “resp” is a function which is responsible to provide a response to the user.

And then we are calling the .end object of the “resp” function so in order to call an object we define a () curly braces at the end of the call.

Related posts
Realtime and Scalable Web Applications

Node.js Twitter API: 5 Steps to Twittersphere Mastery

Realtime and Scalable Web Applications

Using Variables In Node.js: Quick and brief - Logic Latest

Realtime and Scalable Web Applications

Node.js event loops and callbacks: A brief walkthrough

Realtime and Scalable Web Applications

node.js file system: A detailed walkthrough with codes 2022

1 Comment

  • node.js file system: A detailed walkthrough with codes 2022 - Logic Latest
    November 22, 2022 at 3:06 pm

    […] Note -(fs is the name of the variable which holds the filesystem module, according to naming conventions we have written fs in the syntax but we can name a variable with any name we want in our examples we have named the variables as fsys, Read this post about naming conventions and programming basics.) […]

Leave a Reply

Your email address will not be published. Required fields are marked *