Node.js Architecture - Introduction to Node.js

Node.js Architecture - Introduction to Node.js

Daily short news for you
  • How I wish I had discovered this repository earlier. github/opensource.guide is a place that guides everyone on everything about Open Source. From how to contribute code, how to start your own open-source project, to the knowledge that anyone should know when stepping into this field 🤓

    Especially, this content is directly from Github.

    » Read more
  • Just the other day, I mentioned dokploy.com and today I came across coolify.io - another open-source project that can replace Heroku/Netlify/Vercel.

    From what I've read, Coolify operates based on Docker deployment, which allows it to run most applications.

    Coolify offers an interface and features that make application deployment simpler and easier.

    Could this be the trend for application deployment in the future? 🤔

    » Read more
  • One of the things I really like about command lines is their 'pipeline' nature. You can imagine each command as a pipe; when connected together, they create a flow of data. The output of one pipe becomes the input of another... and so on.

    In terms of application, there are many examples; you can refer to the article Practical Data Processing Using Commands on MTTQVN Statement File. By combining commands, we turn them into powerful data analysis tools.

    Recently, I combined the wrangler command with jq to make it easier to view logs from the worker. wrangler is Cloudflare's command line interface (CLI) that integrates many features. One of them helps us view logs from Worker using the command:

    $ wrangler tail --config /path/to/wrangler.toml --format json

    However, the logs from the above command contain a lot of extraneous information, spilling over the screen, while we only want to see a few important fields. So, what should we do?

    Let’s combine it with jq. jq is a very powerful JSON processing command. It makes working with JSON data in the terminal much easier. Therefore, to filter information from the logs, it’s quite simple:

    $ wrangler tail --config /path/to/wrangler.toml --format json | jq '{method: .event.request.method, url: .event.request.url, logs }'

    The above command returns structured JSON logs consisting of only 3 fields: method, url, and logs 🔥

    » 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...
Scroll or click to go to the next page