Node.js is a platform built on Google’s high performance JavaScript engine named “V8”. It is event driven, asynchronous, lightweight and reliable for data intensive applications. Node.js was created by Ryan Lienhart Dahl in 2009.
The main emphasis of Node.js is on Networking in the right way. When working with Node, you can write and execute any kind of JavaScript. If you guys remember, we have a global variable called “window” when executing JavaScript on browser. That’s because we have a window that we are associated with in executing JavaScript on the browser. This browser window relies on DOM (Document Object Model). When you talk about Node, here there are no browser windows essentially. Here, the global variable is called “process”.
You can download and install Node.js from http://nodejs.org/
Once, you are done with the installation. You can open the Node.js prompt where you can familiarize with Node. As said, you can type “process” which is the global variable in Node. You will be able to see properties of the process object such as pid, env, config etc. You can drill down into these properties by typing process.pid, process.env, process.config etc.
Node being built on top of V8 JavaScript engine, we can even execute JavaScript code in Node. Let’s have a look at Node responding to a simple JavaScript function.
Save this JavaScript code into HiAbhishek.js
You can open the Node.js Command Prompt and type the following command to run this JavaScript file.
The result of the execution would appear as “Hi” appearing first and after 3 seconds “Abhishek” appears. Thus, we understand that setTimeout function basically acts in an asynchronous fashion by executing the final statement of logging “Hi” and after 3 seconds executes the log statement inside setTimeout.
This is a great start for exploring more on Node.js. Isn’t it? We wrote some code in JavaScript and executed it using Node. Well, people with experience in JavaScript might feel comfortable with Node but fundamentally there are some differences. Let’s now look at something similar to what we did just now and understand the fundamental difference.
In PHP, you could achieve the same result by
Here also, the result would be “Hi” and after 3 seconds “Abhishek”.
Fundamentally if we analyze the execution, The PHP code puts the CPU to sleep mode where CPU utilization is still there on sleep() and after 3 seconds prints “Abhishek”. If you look at the Node JavaScript code, the control flow went through the setTimeout() understanding that 3 seconds IDLE time has to be achieved in the CPU execution cycle and it just then prints “Abhishek”. That is why in the Node JavaScript code, we saw the log statement after the setTimeout() firing first and log statement inside setTimeout() firing after 3 seconds. That means in Node, the 3 seconds gap where CPU goes idle was put aside to be used for other processes whereas the PHP implementation took the CPU to sleep mode and not in IDLE state. In Sleep mode, execution is stopped or halted for the interval we specify.
Thus, in Node
- YOU NEVER EVER STOP
- NEVER DO YOU SLEEP
- NEVER EVER YOU FETCH URL FROM WEBSITE AND WAIT FOR IT TO LOAD
Therefore in Node, there is no question of stopping anywhere even if you wish to stop. There is no question of waits and locks. The bottom line is that you cannot do anything that halts the execution cycle. You can IDLE the execution cycle but never HALT.
Let’s make a small difference in the Node JavaScript.
The difference in this code is that I replaced the setTimeout() to setInterval(). When we execute the JavaScript in node, we would be able to see that “Hi” is displayed first and then in 3 seconds “Abhishek” is displayed and again after 3 seconds “Abhishek” is displayed and it keeps doing this infinitely.
We have a question at this point. Why didn’t the execution end? As I said, node keeps doing the job as long as it is not directed to stop. The setInterval() method keeps executing every ticks mentioned in the delay parameter. Thus, Node has a natural intelligence to count and know the number of callbacks that are referenced and needs to be executed. When those referenced go away, Node drops out and stops the execution. This ability to remember and count its callback references comes from the powerful V8 JavaScript engine from Google.
Let me remind you that Node is not just JavaScript rather it comes with some more stuff bundled for web server and connections.
Stay Tuned. Hope this blog helped you to understand the basics of Node.