Getting Started with NodeJS [Edition 2022]

Nodejs Banner

The year 2022 has just started and you might have set the goal to master the new technology. When there is the fastest growing and evolving industry, complications of choosing a programming language arise. The advancement of one tech can become the downfall of another and you’re in the chaos of which to master.

If you are thinking “Does learning NodeJS worth in 2022?” then you are in the correct place. Gigantic Open Source Community, cross-platform, lightweight and snappy performance has made Nodejs one of the most preferred technology.

Let’s walk through the journey of getting started with NodeJS 2022 Edition.

Table of Contents

Introduction of Nodejs

Nodejs is a runtime engine that runs on V8 and executes JavaScript outside of the browser. It is influenced by Python’s Twisted and Ruby’s Event Machine which means it’s an event-driven, non-blocking I/O paradigm.

A Nodejs is a single thread and semi-infinite loop, it handles multiple responses in a single process without creating a thread for each response. It quits if there is no work to be done and resumes when the response gets back. Such manners optimize CPU cycles and prevent stopping the thread.

I/O

I/O refers to accessing data from external resources, dispatching data from your application, reading/writing files, network operation and so on.

Non-Blocking I/O

Intensive I/O can be costly and it can prevent running other programs. Nodejs comes in handy for managing thousands of concurrent connections without blocking thread.

For example, User(A) signup on an application, it takes some time to process the credentials, so the user can access it. Likewise, User(B) also tries to signup on it but User(B) has to wait for a while because of the intensive process and single thread nature.

Multiple users trying to signup at the same time can become a catastrophe. Imagine being the 100th user. However, Nodejs handles concerns like these efficiently.

nodejs non-blocking

Event-Driven

Simply it’s a programming model in which the flow of the program is conducted by events. Its commonly used paradigm is the graphical user interface(GUI).

Event-Driven Architecture is the backbone of microservices. Microservices are constructed to perform a specific task on a large application based on an event. Nodejs itself is event-driven hence, it has tons of advantages on microservices.

History of Nodejs

Nodejs was introduced on May 27, 2009, by Ryan Dahl. Upcoming May 27 it’s going to be 13 years old. Compare to Web ( 33 years) and JavaScript ( 26 years ), it is young. Within a short period, Nodejs become a widely used runtime environment.

The invention of Nodejs begins when Ryan Dahl saw the demo on Flickr. A user can upload a photo, it shows the progress bar where the server is responding “3 percentage 5 percentage”.

Uploading file is an intensive I/O, handling multiple requests was complex during that era. It was created to solve problems like – How can you manage multiple things at the same time? What if you never have to wait for the I/O that is processing?

Web 2.0 applications like (Flickr, Gmail etc.) presented what’s the modern web experience should be. As JavaScript became one of the more serious languages, Nodejs came at the right time and become popular among developers.

Popular web frameworks like express, socket.io, fastify and so on are shaping the modern web. NPM worlds largest software registry helped developers to learn, share packages. Node introduced an efficient way to do things on JavaScript server-side development.

In 2022 Nodejs is going to have a massive influence on the below trends. As we know the COVID-19 struck the world, there is increasing demand for real-time applications. Let’s explore what kind of trends in tech are demanding Nodejs.

Full Stack

You probably heard the “Full Stack” term which is quite often a mostly used phrase in the developer community. MERN, MEAN and MEVN are the popular, robust stacks in the web development globe.

Largest tech industries like Netflix, PayPal, Uber are using this stack for building products, which shows its strength and demand of it.

full stack

Web3.0

Metaverse, Blockchain and NFTs have become one of the hottest headlines of 2021. Building decentralized App, Non-Fungible Tokens, Smart Contract applications has become the greatest trend in the tech industry.

Hardhat, Truffle Suite, Etherjs, web3js and so on are the project that focuses on creating open internet or Web3.0.

Web 3.0

Serverless Architecture

Serverless makes the background processing a breeze that helps to focus on business logic rather than on infrastructure. It offers low project expense, better code quality, low module cost and flexibility. Overall, Nodejs is can be used to improve performance for serverless architecture.

Serverless

Internet of Things (IoT)

Nodejs is an excellent source for working with data-intensive applications. It requires less memory and fewer resource to perform a real-time data execution. Hence, Nodejs has the capabilities to build efficient and scalable IoT products.

Internet Of Things

Real-Time Application (RTA)

Real-time applications are the most demanding application in today’s world. People are engaging virtually for various activities like live chats and messaging, streaming games, social media integration and stock exchange analysis.

Real Time Applications

Coding Minimal Node App

Let’s create a minimal node application. In it, we will be working with the HTTP module.

First, install node.js on your machine from here. You can check the node in your machine using node -v in command prompt.

node -v

// output will be similar to this : v14.17.5

Secondly, Create a directory node-app, inside the node directory create an app.js file.

// app.js

const { createServer } = require("http");

const port = 5000;
const host = "127.0.0.1";

const server = createServer((req, res) => {
  res.writeHead(200, {
    "Content-Type": "text/html",
  });
  res.end("Hello world");
});

server.listen(port, host, () =>
  console.log(`SERVER LISTENING TO ${host}:${port}`)
);

We’ve imported the createServer from the HTTP module, createServer creates the new instance of the Server. After that, We are listening to the server using the server.listen method.

The callback function is passed to createServer, whenever the server gets the request the callback function will run. Inside the callback function, we can access the two parameters request(req) and response(res).

The request provides the access to the request header and data. Likewise, the response supplies the data that we are sending to clients.

Finally, Change your directory to node-app and run node app.js in your command prompt and you are ready to go.

cd node-app

node app.js

Here is your first hello world.

Conclusion

Nodejs is an excellent run time environment for the data-intensive application. Active community and open-source flexibility made it grow every year. By the time I’m writing this blog npm has 1,852,070 registered packages which mean you can build almost any kind of project you’ve ever dreamed of. Tech giants are using Nodejs in production, companies like IMB, Microsoft are backing it.

Nodejs offers a great solution for enhancing real-time data, reduces the complexness of learning new languages if you are a frontend developer, supplies low latency and high throughput. It has a powerful foundation, in the upcoming years, the node will evolve even more and disrupt its momentum. It’s never been late to learn Nodejs, “Learning Today is better than Tomorrow” and it will be worth every penny.

What makes you think that you should learn it? Which trends did I miss out on it? Let me know your thoughts about it. Feel free to criticize or roast me in the comments 😁. Peace✌️