A Beginner's Guide to Node.js: What It Is, How It Works, and Its History

A Beginner's Guide to Node.js: What It Is, How It Works, and Its History

Node.js is not a framework or a library, but rather a runtime environment that allows you to run JavaScript code on the server side. This means that you can use JavaScript not only for creating dynamic web pages in the browser, but also for writing backend logic, accessing databases, handling HTTP requests and responses, and much more.

But why would you want to use JavaScript on the server side? Well, there are several advantages of using Node.js:

- You can use the same language for both frontend and backend development, which simplifies your workflow and reduces the learning curve.
- You can leverage the huge ecosystem of npm packages (over 2.3 million) that provide ready-made solutions for various tasks and challenges.
- You can benefit from the high performance and efficiency of Node.js, which is based on Google's V8 engine that compiles JavaScript into native machine code.
- You can easily create real-time applications that support bidirectional communication between clients and servers using WebSockets or Socket.io.
- You can take advantage of the asynchronous and event-driven nature of Node.js, which enables you to handle multiple concurrent requests without blocking the main thread or creating multiple threads.

How does Node.js achieve this? Well, let's take a look at its architecture:

node.js components

As you can see from the diagram above, Node.js consists of three main components:

  1. The V8 engine: This is the core component that executes JavaScript code. It was originally developed by Google for their Chrome browser, but it was later open-sourced and adopted by Node.js. The V8 engine is fast, powerful, and constantly updated with new features and optimizations.

  2. The libuv library: This is a cross-platform library that provides low-level access to various operating system features such as file system operations, networking (TCP/UDP sockets), timers, threads, processes etc. It also implements an event loop that handles all asynchronous operations in Node.js.

  3. The Node API: This is a set of bindings that expose various functionalities of V8 and libuv to JavaScript code. It includes modules such as fs (file system), http (HTTP server/client), net (TCP/UDP sockets), crypto (cryptography), stream (data streams), buffer (binary data) etc.

Now that we have an idea of what Node.js is made of and how it works under
the hood let's briefly go over its history.

Node.js was created by Ryan Dahl in 2009 as an experiment to build a better web server than Apache HTTP Server which he found too limited for handling many concurrent connections. He was inspired by some existing projects such as EventMachine for Ruby and Twisted for Python which used an event-driven approach instead of creating multiple threads for each request.

He decided to use JavaScript as the programming language because he liked its simplicity and flexibility. He also chose to use Google's V8 engine because it was fast
and supported modern features such as closures and anonymous functions.

He presented his project at JSConf EU 2009 where he demonstrated how he could handle 10 thousand concurrent connections on a single process using only 8 MB of memory. His talk received a lot of attention and interest from developers who were looking for new ways to build scalable web applications.

Since then, Node.js has grown rapidly in popularity and adoption by many companies such as Netflix, PayPal, Uber, LinkedIn, Walmart etc.
It has also given rise to many frameworks and tools such as Express, Hapi, Koa, Meteor, Nest etc. that make web development easier and more productive with Node.js.

Node.js has also evolved significantly over time with new versions introducing new features and improvements such as ES6 support, HTTP/2 support, worker threads etc.

Today, Node.js is one of the most popular platforms for web development with millions of developers using it worldwide.

Did you find this article valuable?

Support Dhananjay Thomble by becoming a sponsor. Any amount is appreciated!