Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.
Already enrolled? Login
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?
- Non-blocking (Asynchronous)
- Event Driven (Event Loop)
- Single-Threaded
1- Non-blocking (Asynchronous)
-----> // 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'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.
---------> // 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.2- Event Driven (Event Loop)
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.
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');
}):
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.Get Started
Already have an account? Please Login