How Node.js Work?

By Prajwal Haniya

Tech-letter #12 | March 7, 2023

Node.js is a JavaScript runtime built on top of the Chrome V8 JavaScript engine. It allows developers to write server-side applications using JavaScript, a language traditionally associated with web browsers and client-side scripting. It uses an event-driven, non-blocking I/O model to handle large numbers of concurrent requests without blocking the event loop. It is designed to work with asynchronous programming techniques, such as callbacks, Promises, and async/await. Node.js includes a built-in HTTP server that allows developers to create and run web servers and APIs easily.

Let us just take the above paragraph and get our doubts resolved. This is important because with frameworks it’s a lot easier to start writing code and build APIs but a lot tougher to understand what’s actually happening.

After reading the above lines, You may get a ton of questions to be answered. Below are the ones which I got in my mind and I tried answering them.

What do you mean by runtime environment? What is JavaScript runtime?

A runtime environment typically includes all the software and hardware components necessary to support the execution of the program, such as the operating system, libraries, drivers, and other system resources.

JavaScript runtime refers to the environment in which JavaScript code is executed. It typically includes a JavaScript engine, which is responsible for interpreting and executing the JavaScript code, as well as other components such as the event loop, garbage collector, and other system resources.

There are two types of JavaScript run time environment

What is the difference between the browser runtime and node runtime?

As the names suggest the browser runtime is more client-oriented whereas the node runtime is server-oriented. The browser runtime typically does not have access to low-level system resources, such as file system access, that are available in the Node.js environment.

The Node.js runtime environment, on the other hand, is designed to run JavaScript code outside of the browser, on a server, or another computing device. The Node.js runtime environment provides access to system-level resources, such as file system access, network sockets, and operating system commands, allowing developers to build server-side applications and networked services.

What are server-side applications?

A server-side application is one which runs on a server and performs tasks and responds accordingly to the request done by the client. They can be accessed through web browsers or other client applications, and they often provide various functions, such as managing databases, processing payments, or delivering dynamic content.

What do you mean by event driven model?

In an event-driven model, the program waits for events to occur and then triggers a corresponding response or action. What are events? events are actions or occurrences that happen on a web page or web application. These events can be triggered by user interactions, such as clicking a button, hovering over an element, submitting a form, or other actions, such as the page finishing loading or a timer expiring.

What is an event loop?

According to the nodejs documentation

The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.

The basic idea behind an event loop is to keep checking for events in a loop and handle each event as it arrives.

How does the event loop work?

When an event occurs, the event loop wakes up and processes the event. The event may be like reading or writing data to a socket or calling a function. Once the event has been processed, the event loop goes back to waiting for the next event. If there are no events to process, the event loop may block, sleep, or spin until an event arrives.

Event loops are often used in situations where there is a need to constantly check for new events or data.