Whats Special about Node.js

In this video you will discover what makes Node.js so fast efficient and popular. You will learn about concepts such as Asynchronous, Non-Blocking, Single-Threaded, Event-Driven and Event Loop. Sounds scary? Its really not. Watch and learn now

PAIDLevel: Intermediate12:55 mins
Whats Special about Node.js
You do not have access to this lesson!

Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.

Already enrolled? Login

Course content

Whats Special about Nodejs

Welcome to Learn Node Js in a week course. In this lesson, we are going to talk about what really Node.js is and what makes Node.js so special. And what are the reasons, you should learn it and embrace it's benefits.

What is Node JS

Node.js is a runtime for JavaScript. Let's understand what a runtime is. Every language has a runtime, that runs the language, understands its code and processes the instructions which are written by the programmer. Ruby has a runtime, Python has a runtime, Java has a runtime. Every other language has a runtime.

JavaScript is the most used language on the planet and it did not really have a runtime that ran on the computer. JavaScript could only run on the browser because the browser is the only software that could process the JS code.

So that limited the JavaScript developers, from writing JavaScript code and doing something more powerful that a computer could do, but the browser could not do. That is what Node.js brings us to the table.

Node.js is a runtime for JavaScript so it can run JavaScript code on the machine. Node.js can be installed on any machine, it could be a Linux, it could be on a MAC, it could be on a Windows etc.

What is so special about Node JS? 

There has been other attempts to make JavaScript runtime on the machine, so why did Node.js take off?
Node.js is no the first to do that. The real reason is that Node.js, the type of techniques it implements in processing the JavaScript code is really fast and extremely efficient from the processing stand point.

Properties of Node JS

  • Non-blocking (Asynchronous) 
  • Event Driven (Event Loop) 
  • Single-Threaded
Node.js is non blocking, the way it runs the code by default is asynchronous code. The second thing is that the JavaScript is event driven. It has a concept of event loop, through which it process the code. Also Node.js is single threaded. Let's look into some examples.

1- Non-blocking (Asynchronous)

Below I have a piece of code written

-----> // Synchronous / Blocking <------

var output = getDataFromDatabase('names');
console.log('Hi There');
console.log(output);
In the code above, there are three lines of code. We can create a variable and call it output like var output then we write a function that says getDataFromDatabase and pass a parameter called 'names'

So in essence this line of code var output = getDataFromDatabase('name'); is going to the database, querying the database and getting some data, the data is stored in the output variable. After the first line is processed, it goes to the second line console.log ('Hi There'); and prints that. And in the third line it says print out the output, whatever was stored in the output variable.

The above code is synchronous and in sequence and it's blocking. Meaning until and unless, the first line of code finishes. It is going to block the rest of the code unless the first line is finished.
But Node.js recommends that we do not write our code this way. I'm going to write another piece of code

---------> // Asynchronous / Non-blocking <-----------

getDataFromDatabase('name', function(output){
  console.log(output);
});
console.log('Hi There');
The Asynchronous style of coding is that we define the same function which gets the data from the database getDataFromDatabase pass in the same parameter but we are going to pass another parameter which is a call back function. So the way the above code works is that it will go to this part getDataFromDatabase then get the data and while the database querying is happening which normally takes a few seconds, and it's going to print 'Hi There'  or whatever information is there.
It's not going to wait for database querying to finish, it's going to proceed to the next line. So by design this is non-blocking and asynchronous that means that means everything is happening together.



In an analogy, the example would be for example. There was a waiter at a restaurant, the waiter goes to a table and takes the order, goes to the chef and gives him the order that customer wants to have. After that he sits and waits for the order to be ready, then takes the dishes and serves it to the customer. The waiter, waiting for the order to be ready is what the synchronous way of doing things are.

But the asynchronous way is a smarter way. So while the chef is cooking, the waiter is free and has nothing to do but in the mean time he can server other customers or do some other task a waiter does. So the asynchronous methodology  maximizes the usage of the processor and it's cheaper and efficient for computer processor. 

2- Event Driven (Event Loop)
Another important thing about Node.js is that it's event driven, and it has a concept called event loop. Node.js works in a single thread. It has a single processor. Below you see the list of tasks. Consider it as a single thread. The tasks will be done in sequence, when the first task is done, the second task will start, when the second task is complete, the third task will start and when the third is finished, the fourth will happen and so forth. It will go in a load.
If the new task comes in, it will be added on the top of the task and it's going to be task 5 and so forth



Once it has all the tasks, whenever it has a stack of different task or even a single task, It's going to start doing it. Task 1 from a machine perspective is to store a file, that system is going to be done by the file system. The task could be to store information in the database or retrieve information from a database. Or simply it could be a server request.
When it has sent request to the web request, second one to the database, the third one to the file system, it really doesn't have more work to do. It's not going to wait for the file system to successfully save the files or database to successfully query the things or the web request to completely finish.
So when the Callback comes back, when the file system has completed the task. The results arrived will be stacked on the top of the loop, and whatever printing needs to happen, it will happen.



Another concept that I want to discuss today is called event driven. To explain it further, I'm going to write a piece of code.

---------> // Event Driven <-----------

readStream.on('data', function(Data) {
  console.log('recieved some data');
});

readStream.on('end', function( ) {
  console.log('file ended');
}):
Let's assume there's a content that Node.js is reading from a thing called readStream (we don't have to get into details) so when the data is available or trigger happens from the readStream then the callback function is going to run. If the readStream has ended, meaning the node.js has read all content. Then it will do something else.

In jQuery, we have events, so we have the click event, the load event, do something when the click event happens. Load, click, hover are events that gets triggered and then we do something based on those events. 
I hope you enjoyed this lesson, if you have any question, leave your comments below, I'll talk to you in the next lesson. Goodbye :) 
Become a confident software developer with mentor support!
Get Started
Already have an account? Please Login