How Node.js Work In Simple Way
show the basics of how Node.js work under the hood. ( you should understand this as backend developere)
hi folks, in this blog we will teach a basic understanding of how Node.js works under the hood. hope you like it. before we get started, any issues or miss understood in this blog drop a comment below.
what exactly is Node.js
node.js is an open-source backend javascript runtime environment for easily building fast and scalable network applications. built-in v8 engine allows you to write javascript on the server-side. V8 is there you can see V8 is Google’s open-source high-performance JavaScript and WebAssembly engine, written in C++.
look at this image
Node.js is an event loop single-threaded language. it takes the request from clients and sends them back the response, in the meantime it works in a single thread that will handle the request, the node wants to operate the I/O operation and it uses the concept of threads, the threads are a sequence of task that nodejs needs to perform.
note: I/O refers to Input/Output
The Non-blocking I/O Concept
A non-blocking call in JavaScript provides a callback function that is to be called when the operation is complete. Node. js internally uses operating system level polling in combination with worker threads for operations that do not support polling. Node then translates these mechanisms into JavaScript callbacks.
this method Node.js has the ability to work with multiple requests without breaking one in a single thread, it can initiate data requests from a second client without waiting to respond to the first client, and it can initiate them in parallel.
Event loop
The event loop contains a single thread and is responsible for handling easy tasks like executing callbacks and network I/O. When the program is to initialize all the top-level code is executed, the code is not in the callback function. All the applications code that is inside callback functions will run in the event loop. EventLoop is the heart of node.js. When we start our node application the event loop starts running right away. Most of the work is done in the event loop.
Event queue
As soon as the request is sent the thread places the request into a queue. It is known as an event queue. The process like app receiving HTTP request or server or a timer will emit event as soon as they are done with the work and event loop will pick up these events and call the callback functions that are associated with each event and response is sent to the client.
The event loop is an indefinite loop that continuously receives the request and processes them. It checks the queue and waits for the incoming request indefinitely.
Thread pool
Though node.js is single-threaded it internally maintains a thread pool. When non-blocking requests are accepted there are processed in an event loop, but while accepting blocking requests it checks for available threads in a thread pool, assigns a thread to the client’s request which is then processed and send back to the event loop, and response is sent to the respective client.
- social media:
=> follow me in twitter: alguerocode
=> follow me in github: alguerocode
See My projects, don't forget to add a star is really help me grow.
thank you for reading