What is Node JS? How does Node JS work? Why should Node learn JS?


 Hello friends, welcome to a new tutorial. Today I will try to give a clear idea about Node.Js. Then there will be continuous tutorials on Node.js and by following those tutorials we will learn better about Node.js. I hope to have you by my side all this time. So let's start with today's topic.

What is Node JS?

Many of those who are new think Node JS is probably a programming language or framework! But it is completely wrong. Node JS is not a programming language or framework. So what is Node JS?

Node JS is a JavaScript Run-Time Environment. Okay, but what is this Run-Time Environment again?

Run-Time Environment's job is to run the codes of a specific programming language.

If any of you know Java then you should know about run-time environment. ! [1

Take a look at the picture above, see if you understand anything! If you understand anything then you are Genius! And even if you do not understand, there is no problem! Slowly you will understand and I am with you.

Java usually runs through the Java Virtual Machine (JVM). And to execute those Java codes you need Java Runtime Environment,

This means that the Java Runtime Environment knows very well how to run and work properly with the Java code. I don't want to talk too much about Java because we are now learning Node JS, not Java. I said a little about Java because if you understand this concept of Java, then you can easily understand how Node JS works. I think before you learn a thing, you should understand how it works, then you don't have to go ahead and stop.

How does Node JS work?

We know that JavaScript was created for browsers only. Previously, JavaScript was used only to make websites interactive.
Only browsers could understand JavaScript code. But in 2009 a software engineer named Ryan Dahl brought Node JS to market and changed everything!

With the advent of Node JS, a lot can now be done with JavaScript!

Each browser has a JavaScript engine whose job is to convert JavaScript code to machine code.

Google Chrome has the V8 Engine, Mozilla Firefox has the SpiderMonkey, and each browser has its own JavaScript engine.

The fastest of all is the V8 Engin e and it is open source.

Ryan Dahl had a great idea! He built the Node JS in a mixture of C and C + with the V8 engine.

Just like Java Runtime Environment is needed to run Java code, Node JS is needed to run JavaScript code from outside the browser. Now you understand what is Runtime Environment? Well, let's clear a little more. If we write JavaScript code in Node JS, Node JS converts that JavaScript code to C + code, and that C + code is converted to machine code. Eventually that machine code understands our machine and gives us the output.
This is what happens in the blink of an eye, we don't understand anything.

And what happens in the case of Java is that Java code runs Java Runtime Environment (JRE) and helps Java Virtual Machine (JVM) understand that code and Java Virtual Machine converts that code to machine code, and machine code can be understood by our machine. !

In the same way, browsers convert JavaScript code to machine code by doing C + through Javascript Engine.
Now you can find the match between Node JS and Java Runtime Environment? Now surely this concept is clear to you?
If not cleared yet, all will be clear as you slowly stay with Node JS!

Why should Node learn JS?

Node JS uses event-driven, non-blocking I / O model which makes Node JS lightweight and very powerful

First you need to understand what is meant by this non-blocking I / O?
The full form of I / O is Input / Output.

There are 2 design patterns in programming. One is asynchronous programming and the other is synchronous programming.
Here Asynchronous programming is Non Blocking I / O and Synchronous programming is Blocking I / O.
Node JS follows the Asynchronous or Non Blocking I / O pattern by default. And PHP follows the Blocking I / O pattern.

Let us see a real example of Asynchronous or Non Blocking and Synchronous or Blocking I / O. Follow the code below.

// Say "Hello."
console.log("Hello.");
// Say "Goodbye" two seconds from now.
setTimeout(function() {console.log("Goodbye!");}, 2000);
// Say "Hello again!"
console.log("Hello again!");

Running the above code will give one output in Asynchronous pattern and another output in Synchronous pattern.

First let's see how the Synchronous pattern works! According to the above code, the synchronous pattern that gives the output is:

  • "Hello" will output first.
  • Do nothing for 2 seconds and sit quietly
  • Then after 2 seconds the output will give "Goodbye".
  • It will then output "Hello again!"

Do you understand what that means? Here is the output in the Sequence way. The synchronous pattern cannot perform another task until one particular task is completed. Unable to perform the second task until the first task is completed in Synchronous Pattern.

Our country's education board sites are usually Blocking I / O.
This is why you will see that when the test results are published in our country, many people try to see the results together, as a result so many users can not output at once! One blocks the rest while giving output. Since millions of people try together, it takes a long time to get results.

Now let's see how the Asynchronous pattern would output this code.

  • "Hello" would output as soon as the code was run.
  • Then the output would be "Hello again!"
  • Then wait 2 seconds
  • At the end he would output "Goodbye!"

Here but Sequence is not outputting. When the work is being done it is giving output. That is, not that one cannot do another until one is done.

To put it more simply, suppose 3 customers went to Karim's uncle's shop and ordered 3 cups of tea each.

  • The first customer will eat 4 teaspoons of sugar with tea.
  • The second customer will not eat sugar.
  • The third customer eats color tea.

Uncle Karim here again but not like all other tea shops! He has a special ability! Uncle Karim's specialty is he is Asynchronous.

Uncle Karim is making 3 cups of tea in different ways.

Among the different teas, the third customer's tea was made first as he would eat colored tea and it would take less time to make. Uncle Karim gave the color tea to the third customer as soon as it was made and the customer started eating as soon as he got the tea.
Although the third customer came last and got first.

The rest of the tea is not ready yet so they are waiting.

This time the second customer's tea was made and he got the tea.

After all, the first customer got tea because it took him a long time to make tea. Notice here, Karim's uncle doesn't really have tea before. Because he is non Blocking I / O

Since the node JS follows the Asynchronous pattern so it can work much faster and much more simultaneously at the same time.

Node JS also has NPM, which contains 700,000 Modules. These modules can be used very easily in your project.

There are many more reasons to learn node JS.

In fact node JS cannot be said to be the reason for learning. So start learning Node JS now!

And a full tutorial series on Node JS is going to be published soon.

Say goodbye here like today Thanks and Happy coding! <>

Post a Comment

أحدث أقدم