Node.js Architecture - Introduction to Node.js

Node.js Architecture - Introduction to Node.js

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » Read more

Problem

Check out other articles in this series:

This article series aims to provide readers with an overview of the architecture of Node.js, how Node.js works, and how it helps with organizing and writing better code.

This series is expected to have 4 parts, starting from basics to advanced topics. The content in each part is related, so make sure not to skip any of them.

In this first article, I will talk about Javascript and some basic concepts of Node.js.

Javascript

Javascript is no longer unfamiliar to programmers. It is currently the dominant language on web browsers and widely used for front-end programming. Thanks to Javascript, developers can create websites that optimize user experiences.

Javascript was created by Brandan Eich, an employee of Netscape, in ten days in September 1995. It was originally named Mocha, then it was changed to Mona, and finally became the well-known Javascript.

In 1996, Javascript was officially named ECMAScript. ECMAScript 2 was released in 1998, and ECMAScript 3 was introduced in 1999. It has since evolved into the Javascript we have today, running on all browsers and devices, from mobile to desktop.

As of 2016, more than 92% of websites use Javascript, and it is currently the most popular language on GitHub for projects that utilize Javascript.

Javascript on the Browser & Javascript Runtime

Javascript is widely used in web development, making websites dynamic and highly interactive. Therefore, Javascript code relies on browser support for execution, which is done by Javascript Engines.

A Javascript Engine is used to convert Javascript code into machine code. It acts as an interpreter and consists of the Memory Heap (which stores objects like variables and functions) and the Call Stack (which executes functions).

JavaScript Engine

Most modern browsers use a Javascript Runtime to execute and convert Javascript code using Javascript Engines. Different browsers use different Javascript Engines.

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

Browsers also provide Web APIs that allow Javascript code to access certain objects like window and document, which are not part of the core Javascript language.

Because each browser uses a different engine, they may interpret Javascript code differently, resulting in different outcomes. This is why a website may work well in Chrome but behave abnormally in Internet Explorer.

Therefore, starting from 2016, Ecma International (the standardization organization for Javascript) introduced a naming convention for Javascript versions. ES5 refers to ECMAScript from 2009 to 2015, starting from 2015, versions are named ES6 - 2015, ES7 - 2016, and so on. Each year, the council approves and releases new ES versions. As of now (2021), ES12 is the latest version. Each version introduces improvements and new features, but their implementation depends on whether the browser developers support them or not.

What is the relationship between Javascript and Node.js?

Javascript was originally a front-end programming language and is widely known as one of the most popular programming languages in the world. Node.js was created with the aim of bringing Javascript to server-side development.

In 2009, Ryan Dahl used the fastest Javascript Engine - V8 from Chrome, and made it work without the need for a browser. This laid the foundation for the birth of Node.js. The V8 engine is written in C++ and allows direct execution on the operating system.

Node.js utilizes the V8 Engine to interpret Javascript code into machine code. Since Node.js is designed for server-side development, unlike browsers, some Web APIs such as window and document are not implemented.

It is also important to note that Node.js does not immediately support all features of Javascript (ECMAScript). It requires updates to support new ES features. This means that if you want to use new ES features, you have to wait for Node.js to update.

In summary, Node.js uses Javascript to implement its own code.

Check out the article on how to install Node.js: Installing Node.js and Running Your First Node.js Application.

Is Node.js a programming language then?

No! Node.js is just a runtime environment for executing Javascript code. In fact, Node.js only provides a runtime environment for Javascript code through the V8 Engine from Chrome.

Let's review some 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 executing Javascript code on the server-side.

Node.js is composed of several interconnected components to create a complete environment. The following diagram describes the Node.js architecture:

Is Node.js a programming language then?

In this diagram:

  • V8: The V8 Engine from Chrome, which includes Memory Heap, Call Stack, Garbage Collector, and the conversion of Javascript code to machine code for the operating system.
  • Libuv: An important library that includes the Thread Pool, Event Loop, and Event Queue. It is a cross-platform C library focused on asynchronous I/O tasks.
  • Node.js Standard Library: Includes libraries and functions related to the operating system for Timers like setTimeout, File System like fs, and Network Calls like http.
  • llhttp: Parses HTTP requests and responses (formerly known as http-parse).
  • c-ares: A C library for asynchronous DNS used in the dns module.
  • open-ssl: Provides cryptographic functions used in TLS (SSL), the crypto module.
  • zlib: Compression and decompression operations in sync, async, and streaming modes.
  • Node.js API: Provides JavaScript APIs used by applications.

Conclusion

The content in this article has provided an understanding of Javascript, Javascript Runtime, Javascript Engine, why some websites work well on one browser but poorly in another, the concept of Node.js, and an architectural overview of Node.js.

To understand how these components work, interact with each other, stay tuned for Part 2 of the Node.js Architecture series on 2COFFEE.DEV.

Premium
Hello

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click 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...