A Look into Node.js and Express.js
Published: 2021-12-30 14:36:16

I have recently started my journey into the world of MERN. Everything felt so confusing first two or three weeks. But now, as I dive deeper and explore new sides, they kind of start making sense. So, these are my findings as a beginner. 

## What is Node.js
Node.js is an open-source, event-driven, cross-platform JavaScript runtime environment on which web applications can run. The idea behind Node.js to run JavaScript everywhere, not just limited to the client's browser. It is one of the most popular libraries for creating server-side applications. Just like JavaScript, Node.js runs on the same engine - Chrome’s V8. 

## Why use Node.js?
There are many primary reasons to use Node.js as the back end service provider. Some of them are listed down below:
Node.js is fast and light-weight. 

Node.js is essentially a JavaScript runtime environment and similar to JavaScript, Node.js also makes use of asynchronous callbacks. By default, API calls are asynchronous in Node.js. As a result, Node.js servers are extremely fast and there is realistically no waiting time on making requests.

Applications built on Node.js are highly scalable because of the non-blocking nature of it. This behavior permits Node.j servers to scale to larger applications without any inconvenience whereas traditional multi-threaded servers can only handle limited requests. 

Since Node.js can handle concurrency even though it follows the single-threaded philosophy, it is the best tool to choose for applications that are reliant heavily on I/O, real time, and data-intensive. This is why Node.js is the best suited tool for chat applications, video-streaming platforms etc.

## Node.js Architecture
Node.js is built on the V8 engine which is written in c++. An app running on Node.js and written in JavaScript is actually converted to machine code. After processing the instructions, results are sent back to Node.js and only then Node.js runtime can process the requests.

Node.js is based on Javascript’s single-threaded architecture. So, whenever a request is made, a single main thread handles that request. However, this makes the process blocking. To avoid this issue, Node.js makes use of Event Loop. The event loop constantly keeps track of the tasks that are deemed asynchronous. It is also responsible for inserting the asynchronous task into the queue when it has finished waiting.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxmk8i5mwq8s6zt6ecd2.png)

Besides this one main thread, Node.js can deploy a group of secondary threads, also known as slave threads. These are called worker threads which are useful for extensive disk and network-based async operations. The worker threads complete their assigned tasks concurrently and returns their result to the event loop. 


## What is Express.js
Express.js is a free,open-source, lightweight web application framework for Node.js. This helps organize the web application into an MVC architecture on the server side.

## Why use Express.js
Express.js is a web framework and like other frameworks, it comes with ready-to-use modules that otherwise you would have to write by yourself.  The primary purpose of using Express is to make developers’ life easier. Express lets you  build web applications that can handle multiple different http requests at a specific url. It can listen for requests that come to the server and respond back properly. Besides these, Express can also work with routing and middlewares. Express makes routing simplified and the use of middleware means you are free from tons of work because of the third-party libraries. 

Express provides a lot of built-in utilities that help us speed up the development process. Since it’s a library framework intended for Node.js, Express.js is highly scalable. Express also supports templating engines that enable us to simply generate HTML. Express is not opinionated and does not enforce any “right way” of doing things. As a result, we can use any compatible middleware and structure the app our own way.