Node.js Architecture - Introduction to Node.js

Node.js Architecture - Introduction to Node.js

Daily short news for you
  • A few days ago, Apple introduced a container tool developed by themselves and open-sourced as apple/container. It is used to create and run Linux containers as lightweight virtual machines on Mac, optimized for the Silicon chip line. Does that sound familiar? Kind of like Docker, right? 😄

    Docker on Mac needs to run a Linux virtual machine to use containers, and now with Apple introducing this, maybe Docker will see an update soon. We don’t know how the performance will be yet; we’ll have to wait for evaluations from experts. It’s neither Go, nor Rust, nor C... apple/container is written in... Swift. Wow, that’s quite astonishing. 😳

    » Read more
  • Before, I mentioned that github/gh-copilot is a command-line tool that suggests commands or explains them. While browsing through some "comments," I discovered a rather interesting way of using it that I hadn't expected. That is to set an alias for the command gh copilot suggest as "??" and "???" and then, after getting used to it, it becomes extremely convenient. For example, if you want to compress a directory into a tar.gz format but forget the syntax, you can simply do: bash $ ?? "compress the pages directory into .tar.gz" Or if you encounter a command that is hard to understand, you can use: bash $ ??? "tar -xzvf pages.tar.gz pages"

    » Read more
  • A very nice website that aggregates CLI command line tools that I stumbled upon: terminaltrove.com

    It has a ranking system and is categorized, making it very easy to explore by topics that interest you, feel free to study it 😄

    » Read more

Issues

Hello readers of 2coffee.dev, the Node.js Architecture is a series of articles I wrote a long time ago, from the early days of blogging. Over time, this series has attracted a lot of attention. However, the expression was still vague and not to the taste of readers, so today I have decided to rewrite some articles in this series to express myself better and update a few new pieces of information.

This series provides readers with an overview of the architectural model of Node.js and how it works. This will help readers understand how Node.js executes the written programs. From there, one can learn how to optimize and organize the code better.

The planned number of articles is 4. The contents of each part are interrelated, so you should not skip any articles; please read them in order to follow along consistently. In the first article, I will recap some information about JavaScript, along with the basic concept of Node.js. What is the relationship between JavaScript and Node.js?

JavaScript

JavaScript is no stranger to anyone in the programming community. JavaScript reigns supreme in browsers, being the most widely used language for programming interfaces. Thanks to it, we have the rich web experiences we have today. Many websites focus on interaction and unique user experience to attract users' attention.

JavaScript was created in 10 days by Brandan Eich, an employee of Netscape, in September 1995. Originally named Mocha, it was later renamed Mona and then LiveScript before finally becoming the well-known JavaScript we know today.

In 1996, JavaScript was officially named ECMAScript. ECMAScript 2 was released in 1998, and ECMAScript 3 followed in 1999. It has continuously evolved into the JavaScript we have today, which now runs on most browsers and on devices ranging from mobile to desktop. As of 2016, over 92% of websites used JavaScript, and it currently ranks first on GitHub regarding the popularity of projects utilizing JavaScript.

JavaScript in the Browser and JavaScript Runtime

JavaScript can only run when the browser supports JavaScript. This means that the browser is the entity that decides the execution of JavaScript code. To achieve this, most modern browsers must have a JavaScript Engine, which is a component responsible for interpreting and executing JavaScript code.

The JavaScript Engine plays the role of converting JavaScript code into machine code. The JavaScript Engine functions as an interpreter, comprising a Memory Heap (which stores objects like variables and functions) and a Call Stack (which executes functions).

JavaScript Engine

Creating or producing JavaScript Engines can be costly, and sometimes they may not align with available resources. Therefore, browsers often choose to integrate an existing JavaScript Engine that fits their product development philosophy.

For example, Chrome uses the V8 JavaScript Engine, Mozilla Firefox uses the SpiderMonkey JavaScript Engine, and Microsoft Edge (old version) before using the Chromium engine used the Chakra JavaScript Engine.

Additionally, browsers provide Web APIs that allow JavaScript code to access certain objects like window and document. These are not specifications of JavaScript. They are created by the browser and allow JavaScript to interact.

Because browsers use different JavaScript Engines, they may interpret JavaScript code differently. This leads to situations where some code can yield different results. This is evidenced by a website that runs smoothly on Chrome but behaves unexpectedly on IE or Firefox.

Thus, Ecma International has established rules for naming JavaScript versions, along with efforts to standardize JavaScript in browsers with the hope of eliminating fragmentation. ES5 is the ECMAScript version from 2009 to 2015, and from 2015 onwards, versions have been released sequentially as ES6 - 2015, ES7 - 2016, and so on. Each year, the committee approves and releases the next version. As of the time of writing this article in 2021, there is ES12. Each version brings improvements and new features; however, the implementation depends entirely on whether the browser developers support it.

In summary, despite having a complete specification, whether to adhere to it or not remains uncertain. This has been a persistent issue to this day.

What is the Relationship between JavaScript and Node.js?

JavaScript was initially the most popular programming language for web interfaces in the world. Recognizing the potential of JavaScript, what if developers could use only JavaScript to program servers? Node.js was created with the effort to bring JavaScript to server-side development.

In 2009, Ryan Dahl decided to use the fastest JavaScript Engine - Chrome's V8 - to make it work in a server environment, laying the foundation for the birth of Node.js. Node.js uses the V8 Engine to interpret JavaScript code into machine code. Since Node.js is written for operating systems rather than browsers, some Web APIs for browsers like window and document are not implemented in Node.

It is also essential to note that Node.js does not support all JavaScript features (ECMAScript standards) immediately but must do so through updates. This means that if you want to use new ES features, you must wait for updates.

In summary, Node.js provides a runtime environment for JavaScript outside the browser.

You can refer to the installation guide for Node.js Installing Node.js, Running Your First Node.js Application.

Is Node.js a Programming Language?

No! Node.js is just a runtime environment for JavaScript. In fact, Node only provides a runtime environment for JavaScript through Chrome's V8 Engine.

Below are a few conclusions about Node.js.

  • Node.js is not a programming language.
  • Node.js is not a Framework for server applications; it is a platform.
  • Node.js provides a runtime environment for server-side JavaScript.

Node.js is composed of many interconnected components that create a perfect environment. Below is a diagram describing the architecture of Node.js:

Architecture Model

The components are described as follows:

  • V8: This is the V8 Engine from Chrome, which includes Memory Heap, Call Stack, Garbage Collector, and converts JavaScript code into machine code for the operating system.
  • Libuv: This is an important library that includes Thread Pool and Event Loop, Event Queue. It is a cross-platform C library focusing on asynchronous I/O tasks.
  • Node.js Standard Library: This includes libraries and functions related to the operating system for Timers like setTimeout, File system like fs, Network Calls like http.
  • llhttp: Parses HTTP request/response (formerly known as http-parse).
  • c-ares: A C library for asynchronous DNS used in the dns module.
  • open-ssl: Functions for encryption used in TLS (SSL), crypto module.
  • zlib: Compression and decompression functions that can run synchronously, asynchronously, and streaming.
  • Node.js API: Provides JavaScript API used by applications.

Conclusion

Node.js is not a programming language but a runtime environment for server-side JavaScript, built on Chrome's V8 Engine. With the ability to convert JavaScript code into machine code and provide distinct APIs, Node.js enables developers to extend the use of JavaScript beyond the browser, facilitating the development of modern server applications. The architecture of Node.js consists of several crucial components such as V8, Libuv, Node.js Standard Library, llhttp, c-ares, open-ssl, and zlib, each component performing specific tasks to ensure performance and flexibility. Notably, Libuv plays a core role with Event Loop and Thread Pool, helping Node.js efficiently handle asynchronous I/O tasks.

This is just the introduction, and the subsequent parts of the series will delve deeper into how the components of Node.js interact to create a complete system. Please stay tuned to explore more details about this powerful technology.

References:

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...