What is Node.js?
Published: 2022-10-30 21:38:22

JavaScript had the reputation of being a toy language. A tool to add interactivity to web pages. For a long time, its use cases were limited to the browser, but the creation of Node.js completely changed its role in the modern web development world.

Node.js allows us to run JavaScript outside the browser, and out of nowhere, a limited language became the favorite companion for thousands of developers working on small and big projects.

Its vibrant community created many tools that power many applications on servers, desktops, and embedded systems.

- Node.js is commonly used with Express or Fastify to power complex backend services.
- Electron package Node.js and Chrome to create cross-platform desktop applications. It is used in production to power programmers’ favorite editor VSCode and messaging applications like Microsoft Teams.
- Embedded systems can leverage low.js to develop applications.

## Why Node.js?

### Before Node.js

Contrary to popular belief, Node wasn’t invented to allow JavaScript outside the browser. The latter was a positive outcome. It was developed to create more performant web servers.

Ten years ago, handling multiple user connections involved the creation of a new thread, an independent unit of execution within the web server process. This methodology implied two problems:

- If you perform a database query, it blocks the entire thread from executing other operations until it receives the result.
    

![blocking execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kjd6aumd2jb16gnzn34a.png)
    
- Handling concurrent connections with associated threads requires high memory consumption and increased difficulty in managing synchronization between them.

### The Node.js way

JavaScript and Node.js solve the problem of threads' high memory usage and synchronization by using a single thread paired with an event loop mechanism. 


![non-blocking execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo7tgkn71flt5dadb0zj.png)

In the code snippet above, Node.js can handle concurrent connections and run other pieces of code because the database query execution is delegated to the event loop. The database operation has a callback function which is run when the event loop notifies the single thread that the query is completed.

![single thread running the query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj5xy47mwvliwnq2o6o1.png)

## Node.js internal overview

To understand Node.js, we need to understand V8.

V8 is a C++ program that runs JavaScript code and converts it into instructions understandable by the computer microprocessor. V8 is included in Google Chrome and other browsers to run our websites JavaScript and enable our rich web experience.

The V8 engine is not limited to modern browsers but can also be embedded into any C++ program and extended with more functionalities. Not by chance, Node.js is a C++ program that integrates the V8 engine and other libraries to enrich JavaScript with new features:

- libuv, the C++ library responsible for the event loop, TCP and UDP connection sockets, DNS resolution, file system operations, data encryption, etc.
- C++ bindings are responsible for invoking C++ code when running Node.js-specific JavaScript code.
- The JavaScript core API.

![node.js internals overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4z7ypdgiul0y3ddvt1p.png)

## Conclusion

Node.js clever design and event loop has empowered thousands of developers and products to create web servers capable of handling thousands of concurrent connections without the hassle of threads’ high memory consumption and synchronization. Node.js event loop capabilities are not limited to database queries and concurrent connections, but they are also used in production to process large files, encrypt/decrypt data, perform network requests, etc.

You will explore in more detail the event loop in the following article.

_If you liked the article, follow us on Twitter [@fabrizio.lallo](https://twitter.com/FabriLallo) and [@AndrewHu368](https://twitter.com/AndrewHu368)_