Realtime and Scalable Web Applications

Node.js http server – Part 1 of the complete Node.js bootcamp

nodejs bootcamp

Welcome to the Part 1of Node.js complete, fast paced and in-depth bootcamp. In this bootcamp we will learn about node.js, what it is, what are its benefits, how can you create large scale enterprise applications and indepth coverage of all the elements of node.js in a rapid and easy to learn structure. We will learn to implement a Node.js http server. Throughout the course we will learn by practising and designing a large scale backend API and learn about its deployment in production.

What is node.js?

Node.js is a runtime environment and library for running JavaScript applications outside of a browser. It includes a built-in HTTP server for serving dynamic content. Node.js also includes modules, which are libraries of code that can be used in your applications. It’s used by large companies such as Netflix, Walmart, and The Guardian for mission-critical applications. In this article, we’ll show you how to install Node.js modules for your project. We’ll also show you how to deploy your Node.js application to a production server. With its fast, asynchronous I/O and support for a variety of programming languages, Node.js has emerged as the preeminent choice for developing web applications.

What is node.js used for? What are its beneficial features?

Node.js is an amazing platform for developing web applications. With its fast, asynchronous I/O and support for a variety of programming languages, Node.js has emerged as the preeminent choice for developing web applications. One of the most powerful features of Node is its vast ecosystem of modules that allow you to easily extend its capabilities.

Due to being designed as a non-blocking IO farmework which allows you to process several requests at the same time. This new model/framework allows you to make an application significantly more scalable with less RAM and less other hardware resources. To handle incoming requests, the requests are queued and executed, permitting your application to utilize RAM much more efficiently when executing asynchronous requests. As a result, your software or the API will use much less system RAM and CPU, achieves high processing levels, and your application will execute large operations in less amount of time.

JavaScript is one of the most common programming languages that front-end developers program in and has wide support/knowledge-base and a relatively easy learning curve. 

In API development, Building APIs with Node.js is extremely simple. An API developer does not need to worry about standard processes across industries, because API frameworks standardize development processes according to the target industry or requirements. With Node.js, very little API development is involved, because it preempts standard development processes across multiple interfaces.

Using Node.js is worthwhile for deploying applications based on although its potential to Internet-native APIs.

APIs which allow users to retrieve entire database contents as a call is not liked by users who never want the web to merely roll out hermetically sealed databases. A smart API will be the type that puts an upper limit on the data items displayed and Node.js allows us to achieve this. This helps cut down the data resources needed to generate and render the app.

The REST APIs developed through nodejs are also liked by clients for their filtering and pagination. They organize content into sections and provide access to the pages needed with customizable filtering, sorting and performing complex query operations.

For more details about the architectural design on how you can deploy large scale enterprise apps see this.

What is MongoDB?

What is MongoDB? MongoDB is a cross-platform document-oriented database system. Classified as a NoSQL database system, MongoDB eschews the traditional table-based database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls them “collections”). MongoDB boasts wide range of features in a vast range including:-

Database replication sets which will be able to automatically generate and update copies of database regardless of whatever locations it is hosted.

Special features like primary and secondary replica set failover, if one database server fails some other server becomes the primary provider among the replica sets.

Has varieties of aggregation pipelines to combine and form complex queries by joining multiple queries.

Has powerful indexing options which drastically reduces time taken for a particular query to respond.

Install Node.js for your platform 

To install Node.js on a Windows machine

Download the Node.js installer from the node.js official page 

node.js official download page

You may want to install the LTS version for better compatability and support features for long time period without needing it to reinstall again and again.

Download the installer for your system architecture for Windows.

Run Installer and follow all steps

To begin the installation process, right click on the installer file you downloaded, and click on run-as-administrator to start the process.

Follow on the installer wizard and click on the next on every page. Accept the terms and conditions.

nodejs setup wizard
accept terms and conditions, as usual

Choose the Node.js runtime in the custom setup box.

follow through

For Tools for native modules you may check on the box automatically install the necessary tools. Click next

click next

After the wizard completes click on finish button.

finish installation

if you choose to install necessary tools in step 4

follow through the extra tools installation guides

The installer will then relocate the node binary to the system’s directory by default, as required for server use.

For Mac download the installer for the Mac from the node.js official page

Follow all the installation steps as denoted below in the images and finish the installation.

Just follow, agree and select next on each dialog box
after completion click finish

A basic demo Hello world Application

To understand the HTTP sever, we’ll create an implementation that runs on a remote network host listening on a specified port number, which creates a web page for the local client(local client in this case refers to you) from your operating system. In this example, we’ll use port number 8080. This port number is not mandatory and you can use any port number.

The method http.createServer() is a Node.js core module (a module included in Node.js source, that does not require installing additional resources). The http module provides the functionality to create an HTTP server using http.createServer() method. To create an application, run the following command through the command prompt. # node # http.create

const http = require('http')

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

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



Explaination of above code

“const http = require(‘http’)” imports the http module to create an http server

“http.createServer((req, resp) => {“ this creates an http server with the createServer function, this is a callback function (a callback function is denoted by “=> “sign  followed by { . 

The { or a curly braces creates an abstract code which means that the code written after this block is valid for the corresponding function only. In this case http.createServer function which is a callback function. This callback function takes in two parameters “req” which denotes the incoming request and “resp” denotes the response to be given. Note that “req” “resp” are just conventional names and you can choose these parameters of any name you want.

After creating the server

resp.writeHead(200, {

        ‘Content-Type’: ‘text/plain’


This code writes a response in this case with 200 which is the http status code which signifies that response is valid and OK and there were no issues processing the incoming request. The  ‘Content-Type’: ‘text/plain’ means the response written on the webpage is in plain text.

resp.end();  denotes completion of the response.

}).listen(8080), note that the “http.createServer((req, resp) =>” started with a curly bracket “{“ and is closed with “}”  and listens the incoming request and creates a http server on 8080 port.

the round bracket which started after  http.createServer“(“ is the main code block which ends with the }).listen(8080)m round bracket denoted here followed by specifying the listening port.

To run this program:-

  1. Save the file with any name, in this case, we type “hello.js” and press Enter.  in your command prompt in windows and inside terminal in the Mac(in the terminal/command prompt come to the default directory where the hello.js file is located for example if its in Documents folder, navigate to that folder cd Documents and in my case its in Documents/hello world folder) 
  1. First initialise the npm modules, you can do this by “npm init” command and press yes to every question or type the details. (note that I am writing the commands in inverted commas, when you type these commands the inverted commas are not needed)
first initialise the node.js modules by “npm init” then install http module by command “npm i –save http” as depicted above
  • After that install the http module by command “npm i —save http” this will install the http module framework which is required to run the http server, we write —save to save it in the local environment.
  • Type the command “node hello.js” which should result in the application printing “Hello world!”
running the hello world program
  • Perhaps in this case using the localhost like this and type this url “http://localhost:8080” in your default web browser.
  • If you open and look at this webpage, you will see a “Hello, world!” message appear like in this image below.
final output of the program
  • You can afterwards close the application by pressing CTRL + C.

In the next walkthroughs we will learn about routing, middleware, details about all of the http status codes, and some more programming examples.

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

Leave a Reply

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