NodeJS

Fathima Zihara Iqbal
6 min readMay 13, 2022

What is NodeJS?

NodeJS or Node is an open source and cross platform runtime environment for executing JavaScript code outside of a browser. Quite often Node is used to build back-end services also called API’s (Application Programming Interfaces). These are the services that power our client applications like a web app running inside of a web browser or mobile app running on a mobile device. These client apps are simply what the user sees and interacts with.

Node is ideal for building highly scalable, data-intensive, and real-time backend services that power our client applications. Given below are some advantages of using Node although there are other tools and frameworks for building backend services such as ASP.NET, Rails, Django and so on.

  • It is great for prototyping and agile development.
  • It can be used for building superfast and highly scalable services. It is used in production by large companies such as Uber, PayPal, Walmart, Netflix and so on.
  • JavaScript is used in Node applications.
  • Since JavaScript can be used both on the frontend and on the backend, source code will be cleaner and more consistent. So, the same naming conventions, the same tools, and the same best practices can be used.
  • It has the largest ecosystem of open-source libraries available.

How NodeJS works?

Node applications are highly scalable because of the non-blocking or asynchronous nature of NodeJS.

What does asynchronous mean?

A single thread is allocated to handle an arrived request. But rather than waiting for the completion of that task to go to the next, that thread will be available to handle the request of another client. This is known as non-blocking or asynchronous architecture, and this is how Node applications work. Hence, a single thread is used to handle multiple requests.

Node applications are asynchronous by default. If we need to query a database, a threat doesn’t have to wait for the database to return the data. While the database is executing the query that thread will be used to serve another client. When the database prepares the result, it puts a message in the event queue. NodeJS is continuously monitoring this event queue in the background. When it finds an event in the queue it will process it. This kind of architecture makes Node ideal for building applications that uses a lot of disk or network access. NodeJS allows to serve more clients without using additional hardware resources and thus Node applications are highly scalable. Since Node applications are single threaded, NodeJS should not be used for CPU intensive applications like a video encoding or an image manipulation service. In CPU intensive applications, there are lot of calculations that should be done by CPU and few operations that are related to file system or the network. Thus, NodeJS should only be used for building data intensive and real time applications.

In contrast to nonblocking or asynchronous architecture there is blocking or synchronous architecture. Once a single thread is allocated to handle a request, it will wait for the completion of that task to go to the next, that thread will be unavailable to handle the request of another client. This is known as blocking or synchronous architecture and that’s how applications built with frameworks like ASP.NET or Rails works by default.

In synchronous architecture imagine what would happen if there were large number of concurrent clients, at some point we’re going to run out of threads to serve these clients (i.e., all threads are busy). Accordingly, new clients have to wait until any of the threads are available. If we do not want clients to wait, then add more hardware resource. Hence resources are not utilized efficiently in this kind of architecture. This is the problem of blocking or synchronous architecture. Though it is possible to use asynchronous architecture in ASP.NET, it is required to do extra work for that.

How to install NodeJS?

If you’re on windows open command prompt. If you’re on Mac or Linux open the terminal. Initially check if you have Node on your machine or not.

Hence, run ⬇️⬇️

node --version

If it shows a version, then Node is already installed. On this pc I’m running Node version 14.17.6 which is an earlier version of Node. The latest stable version is v16. Let’s install the latest version of Node. Open the browser and browse nodejs.org.

In the home page there are two versions for Node. One is the latest stable version (LTS) which is recommended for most users at the time of creating this blog i.e., version 16.15.0 and there is always a newer version which is experimental, and it might not be stable. In the future when you’re reading this blog there is possibility for the latest stable version to be newer. In that case install the latest stable version. Now to install the latest stable version click on v16.15.0. Then it initiates downloading Node installer. Once it is downloaded open the installer.

Choose “next” in the welcome screen.

Accept the terms in license and then continuously choose “next” until it gets installed.

After the installation choose “finish”.

Now back in the command prompt run “node --version” again to test whether NodeJS is upgraded correctly to version v16.15.0.

Build the first Node application

Now we’re ready to build our first Node application. Initially create a new folder named first app. Open that folder inside the IntelliJ IDEA 2021.3.2 Ultimate Edition which is my code editor. You can use Visual Studio code, Sublime, Atom, or any other editors. Add a new file named app.js in the created folder.

In this file we can write regular JavaScript just like the JavaScript that we write for the browsers. So, define a function sayhello that takes a parameter called name and simply logs a message on the console “Hello name” and then we can call this function.

Now to execute this code go back to the terminal and run Node and pass the name of the file as an argument (app.js). Node includes Chrome’s V8 JavaScript engine. File app.js is passed to Node and then Node gives it to V8 for execution. “Hello Zihara” is printed on the console.

That’s it for this article, thank you so much for reading. Feel free to express your thoughts in the comment section. Explore more about this topic.

--

--