Building your first web apps with Node js

 

Node.js Innovative Web Apps

 

Node js is an powerful open source server side runtime environment built on Chrome's V8 JavaScript engine. It provides an event driven, non-blocking I/O and cross-platform runtime environment for building highly scalable server-side application using JavaScript. Node js can be used to build different types of applications such as command line application, web application, real-time chat application, REST API server etc. However, it is mainly used to build network programs like web servers, similar to PHP, Java, or ASP.NET.

 

It's very important to have a clear idea about Node JS architecture, what type of processing model it is following, How it handles concurrent request with Single-Threaded model and many more.

 

Let's have a small look into the Architecture Level:


Node.js is mono thread or single thread. This means that there is only one process and one version of the program that can be used at any one time in its memory. Many questions arises stating that whether Node.js is really fast because it could manage loads of requests simultaneously. If it’s mono thread, can it only perform one action at a time?

So we have quite simple answer for this i.e Yes it is much faster ,it can only do one thing at a time and therefore only runs on one processor core. moreover, it does it efficiently, despite the latter, and is still much faster. This is mainly due to the event oriented nature of Node.js. Apps that use Node never sit around doing nothing. Whenever there is an action that takes a little too long, the program hands over to Node.js, which will carry out other actions while waiting for the event to happen to tell it that the operation is finished.

 

Single Threaded Event Loop Model Processing Steps:

  • Clients will send request to Web Server.

  • Node JS Web Server internally maintains a Limited Thread pool to provide services to the Client Requests.

  • It receives those sent requests and places them into a Queue. It is known as “Event Queue”.

  • This Node js Web Server internally has a Component, known as “Event Loop”. Why it got this name is that it uses indefinite loop to receive requests and process them.

  • Event Loop uses Single Thread only. So Loop checks any Client Request is placed in Event Queue. If no, then wait for incoming requests for indefinitely.

  • If yes, then it will pick up one Client Request from Event Queue and will the following task:

 

    a) Starts processing that Client Request.

    b) If that Client Request Does Not requires any Blocking I/O Operations, then process everything, prepare response and send it back to client.

    c) Sometimes this client request requires some Blocking input output Operations like interacting with Database, File System, External Services and many more then it will follow different  approach:

      - Checks Threads availability from Internal Thread Pool.

      - Picks up one Thread and assign this Client Request to that thread.

      - That Thread is responsible for taking that request, process it, perform Blocking IO operations, prepare response and send it back to the Event Loop.

      - Event Loop in turn, sends that Response to the respective Client.




Node.js may be monothread, but it is flexible because of events

System Requirement:

  • Node Js packages are written in c so you must have a c compiler such as GCC on your server.

  • Python 2.7 or later.

  • If you are using encryption in your networking code then you need openssl Cryptographic library.

 

Now we will guide you with simple steps to setup this Stable Node Js Environment on your server:

 

  1. First you need to do ssh login to your server via putty and make sure that all the required packages are installed in the server if not you need to install it before installing the node js.

 

        curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -

 

        Next To compile and install native addons from npm you may also need to install build tools:

        yum install -y gcc-c++ make

 

        Once you done with it install node js using the below command:

        sudo yum -y install nodejs

 

        To manage the node js process you need to install pm2 using the command below:

        npm install -g pm2



  1. Once first steps gets completed check whether nodejs is installed correctly or not :

 

        node --version

 

  1. Next you need to enable the port to run node js on your server. Most of the developers use port 3000 so we have taken port 3000 to get enabled here so kindly contact your hosting provider for enabling the port.

 

  1. Create node js file under any server directory where you need to add your complete project files.

        For example, we have created a node js file and named it as mypage.js

 

        var http = require("http");

        http.createServer(function (request, response) {

        response.writeHead(200, {'Content-Type': 'text/plain'});

                response.end('Welcome To HostingRaja\n');

        }).listen(3000);

        console.log('Server running at http://127.0.0.1:3000/');

 

  1. You can run the node js application using the command:

        pm2 start filename.js

 

        To know more about list of applications currently managed by PM2 use:

        pm2 list

 

        To Monitor pm2 process like cpu, memory usage, app status use the below command:

        pm2 monit

 

Sample Output of node js page looks as shown below: