🚀 Node.js Understanding: An Inside Look

Abdul Mohiz
3 min readApr 13, 2024

--

Ever wonder how Node.js manages all the file system operations and network requests in your applications? Let’s explore its internal architecture briefly to see how the magic works behind the scenes.

Node.js: JavaScript Unleashed Outside of Browsers

Node.js made it possible for JavaScript to operate outside of web browsers, which completely changed server-side programming. But given that JavaScript is a high-level language and does not have direct access to a computer’s integral functions, how can it accomplish this with?

Enter LibUV: The Backbone of Node.js

At the heart of Node.js lies LibUV, a powerful C++ library that provides the groundwork for Node.js to perform tasks that would otherwise be impossible. LibUV handles crucial operations such as asynchronous I/O, event loop management, and cross-platform abstractions.

When a request comes in, LibUV, which has access to the computer’s internal features like networking, listens to the request and calls the callback function. Once the callback function runs and obtains the result data, it sends the data back to LibUV, which can then return it to the client who made the request.

Let’s say we start a server inside Node.js using the HTTP method. We are instructing Node.js to listen to HTTP requests at a certain port.

const http = require('http');

function doOnIncoming(request, response) {
response.end('Hello Node.js');
}

const server = http.createServer(doOnIncoming);

server.listen(3000);

Here is the breakdown of each line:

const http = require('http');

This line imports the built-in Node.js http module, which provides functionality for creating HTTP servers and handling incoming requests.

function doOnIncoming(request, response) { ... }

This line defines a function named doOnIncoming, which will be invoked whenever a new HTTP request is received by the server. It takes two parameters: request and response, which represent the incoming request from the client and the response object that will be sent back to the client, respectively.

response.end('Hello Node.js');

This line sends the string 'Hello Node.js' as the response body to the client, indicating that the server has successfully processed the request. It also ends the response, indicating that no further data will be sent.

const server = http.createServer(doOnIncoming);

This line creates an HTTP server using the http.createServer() method, passing the doOnIncoming function as the request listener. This means that whenever a new HTTP request is received by the server, the doOnIncoming function will be called to handle it.

server.listen(3000);

This line instructs the HTTP server to start listening for incoming requests on port 3000. Once the server is listening, it will begin accepting incoming connections and invoking the doOnIncoming function to handle them.

Exploring the Request Object in doOnIncoming Function:

The request object encapsulates information about the incoming HTTP request. It provides access to key details such as the request URL, HTTP method, and request headers. Let's take a closer look at some of the methods available on the request object:

request.url: Returns the URL string of the request.

request.method: Returns the HTTP method (e.g., GET, POST) of the request.

request.headers: Returns an object containing the headers of the request.

request.body: Used to access the body if POST requests.

Understanding the Response Object in doOnIncoming Function:

On the flip side, the response object enables the server to send a response back to the client. It offers various methods for setting response headers, writing data to the response stream, and ending the response process. Here are some of the essential methods available on the response object

Now when we start the server and hit the http://localhost:3000/ on our browser we’ll get the Hello Node.js response.

Node.js is a powerful tool, and libuv is just the tip of the iceberg. In future posts, we’ll dive deeper into Node.js’s core features and explore how it can take your web development skills to the next level. Stay tuned!

--

--

Abdul Mohiz
0 Followers

Software Developer, always learning, exploring, and building.