Node Js — Introduction

Lokesh
The Startup
Published in
4 min readJun 18, 2020

If you are javascript developer, till now you might have used it only for client side operations. Now what if you can use the knowledge of javascript to write code for server side interesting right ? NodeJs allows you to write server side code using javascript. NodeJs is runtime based on chrome’s V8 engine.

Why NodeJs ?

* NodeJs is Javascript runtime based on chrome’s V8 engine.

* NodeJs is event-driven, non-blocking IO model, that makes it light weight and more efficient.

What is event-driven?

Event driven means there is generally a main loop that listens for events and then triggers the callbacks based on the type of event emitted. NodeJs follows observer pattern. So when we create an event and event listener, whenever the event is triggered the event listener is called. Let’s understand with an example.

var events = require('events'); //importing the events module
var eventEmitter = new events.eventEmitter(); //create the object
var connectHandler = function connected()
{
console.log('connection succesful.');

// Fire the data_received event
eventEmitter.emit('data_received');
}
eventEmitter.on('data_received', function() {
console.log('data received succesfully.');
});

As we can see in the above code snippet,

  • We import the events module.
  • Create a object for event emitter
  • Create a event handler and from that we fire a event named ‘data_recieved’, which we defined below.
  • When the ‘data_recieved’ fired we log ‘data received successfully’.

What is Non-blocking?

NodeJs projects are highly scalable because they are non-blocking, asynchronous. NodeJs is a single threaded system. One thread means it can do only one thing at a time. Now you can think that if there is only one thread then what if there are 100 concurrent users, the next user have to wait until the previous user request is responded. So this approach will not be efficient one. So to optimize this we have the concept of Blocking and Non-Blocking.

Let’s assume that we have a single thread that can do only one task. And we have 3 tasks to be completed T1, T2, T3. T1 is file access, T2 is http request and T3 is file IO. So now thread is working on T1, then it can not perform the task T2 until the T1 is completed if we use blocking model, but when we use non-blocking model then it can start T1 and while the T1 is taking some time to execute the thread can work on T2, and T2 is also taking some time then it can execute T3. When the T1 is completed the call back function can be called and it can give the results. so in this way the non-blocking model saves time and execution time is faster.

For better understanding lets see an example, You are going to a restaurant and you have a waiter to serve you. You order something and the waiter gives that to the chef for processing. Now if the model followed for waiter is blocking then the waiter will wait until the food is prepared and serve you then only he can attend the next customer, if the non-blocking model is followed then the waiter takes your order, gives it to chef and then he can attend other customers once your order is ready he can serve it to you.

Event Loop

Let’s understand the working of Event loop with an example,

Here in this example we have 2 console.log and one call to get the api’s. So let us explore how this works with single thread model. The code shown in the image is executed in following steps with async programming,

  • Main function is loaded in the stack
  • Then the ‘hi’ is printed in the console.
  • Next we have a api call, it moves to stack, since this is not a part of V8 code base(When you explore the code base of chrome’s V8 engine we do not have this .get, set timeout, etc. They are part of browser’s web api).
  • So the API call is moved to the WebAPI stack.
  • Our thread is now again free and it moves to the next execution i.e it prints the ‘JSConfEU’.
  • Once the API is processed in WebAPI stack it returns the callback to task queue.
Move the call back to task queue
  • When the call stack is empty then the callback function in task queue is executed and we get the response of object.
Execution of call back function

This approach is fast when compared with the synchronous blocking model.

--

--