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 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 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).
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.
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.
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 composed of many interconnected components that create a perfect environment. Below is a diagram describing the architecture of Node.js:
The components are described as follows:
setTimeout
, File system like fs
, Network Calls like http
. dns
module. crypto
module. 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:
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!
Subscribe to receive new article notifications
Comments (0)