Chapter - 0
Hello guys, welcome to the fresh and new series on node JS, in this series, we will work on node JS from basic to advanced. So if you are planning to work on the same, these blogs will be very helpful for you.
In Chapter 0 we will see What is node JS? How it is created? It's internal working and installation part. It would be beneficial for you if you would make short notes side by side for future reference
What is Node JS and how it is built?
Node JS is a javascript runtime environment built on a Chrome V8 JS engine.
Earlier we could run JS only in the browser with the help of the JS Engine which is present inside the browser
Chrome -> V8 (Most Popular) (Written in C++)
Firefox -> Spider Monkey
Safari -> JavaScriptCore (also known as Nitro)
Ryan Dhal combined the V8 engine with the C++ which resulted in the formation of Node JS
Now we can run the JS outside of the browser
JS can talk to native machines because of C++
We can also create web servers in the JS language
It is a cross-platform, open-source maintained by Open JS Foundation
Inside working of Node JS
1. Clients and Requests:
Client-1 and Client-2 represent users or applications sending requests to the Node.js server.
Request-1 and Request-2 are the tasks or operations these clients want the server to perform (e.g., reading a file, or accessing a database).
2. Event Queue:
When a request is received by the server, it is placed in the Event Queue.
The Event Queue is like a waiting line where all incoming requests are stored until they can be processed.
3. Single Thread Event Loop:
Event Loop is the core part of Node.js. It continuously checks the Event Queue to see if there are any tasks (requests) that need to be processed.
Node.js operates on a single thread, which means there is only one main path of execution that handles all tasks. However, it is highly efficient because it doesn't wait for tasks to complete before moving on to the next one.
4. Non-Blocking I/O Requests:
The Event Loop checks if a request is non-blocking (does not require waiting, like reading from memory).
If the request is non-blocking, it is processed immediately within the Event Loop, and the response is sent back to the client. This allows Node.js to handle many requests very quickly without waiting.
5. Blocking I/O Requests:
If the request is blocking (requires waiting, like accessing a database or reading a file from disk), the Event Loop cannot process it immediately.
Instead, it sends the blocking request to a Thread Pool (handled by libuv, a library used in Node.js).
6. libuv Thread Pool:
The Thread Pool contains multiple threads (e.g., T1, T2, T3, etc.) that can handle blocking tasks.
When the Thread Pool finishes processing a blocking request, it sends the result back to the Event Loop.
7. Sending Responses:
- Once the Event Loop receives the response from the Thread Pool (for blocking requests) or processes it directly (for non-blocking requests), it sends the response back to the appropriate client.
8. Efficiency:
- By using this approach, Node.js can efficiently handle many client requests at once without being slowed down by long-running operations. Only the tasks that need waiting (blocking I/O) are sent to the Thread Pool, while other tasks are handled immediately, keeping the system responsive.
Installation of Node JS
Go to the browser, search nodejs.org and download the LTS (long-term support) version for your OS
Fact: NodeJS follow the odd-even version rule -> odd is for the current version which has some bugs and even for LTS which is stable.
To check whether the node is installed:
$ node --version
$ npm --version
And that's all! You are now ready to work on Node JS!
Subscribe to my newsletter
Read articles from Kartik Sangal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by