What is Nodejs?

September 22,2020 - 10 min read
What is Nodejs?

Nodejs is a powerful Javascript framework used to develop

  • server-side applications
  • networking applications in distributed systems
  • real time web applications
  • ones interacting with hardware
  • and other common purpose software.

Applications built with the help on Nodejs is fast and highly scalable. Nodejs can generate dynamic content on webpages, it can collect data produced through forms and add, delete or modify the database you are using in your project.

This Javascript framework is built on Chrome’s V8 engine written in C++. It is used as a compiler and it executes the code at an impressive speed which leaves no chance for buffering or time lag. It uses Javascript as its scripting language and runs on the V8 environment.

Nodejs is a cross platform runtime environment that means it is capable of running on various platforms like windows, LINUX, UNIX etc.

The architecture of Nodejs is single threaded and asynchronous in nature. We would learn more about in detail later in this article itself.

One important thing to note is that Nodejs is open source and free. Anybody can use it for free of cost and can contribute for its development.

Now that we have a brief overview of what is Nodejs, let’s dwell into the technical details.

What is Nodejs architecture?

To begin with Nodejs let’s start by understanding the client-server architecture.

The client server architecture basically consists of users, servers and database. This architecture facilitates users to send and receive requests and responses through a centralized server.

The client server architecture can perform its function using multiple threads or using one single thread. It is categorized into two types multi-threaded and single threaded.

In a multi-threaded client server architecture, each request is handled by a separate thread. Whereas in a single-threaded model (also called event-driven model), an event is created in response to each request generated. The events are executed using an event loop which is single-threaded.

Nodejs is a single-threaded architecture.

There are several advantages of single threaded architecture over multi-threaded architecture.

  • Improved Response Time

    In a multi-threaded architecture, if the thread pool is dried off then the incoming requests have to wait. This adversely affects the response time of the model. In event driven architecture there is only one thread that takes all requests hence there is no such problem.

  • No Block Request

    Multi-threaded model processes synchronously, that means each request is processed only when all previous requests are processed. While Single threaded model executes asynchronously, all requests will be entertained simultaneously.

  • No Context Switching

    In a multi-threaded model, there are lots of thread at work and hence there would be a lot of context-switching. Context switching decreases the speed of execution. In a single threaded model, there is only one thread and hence there is no concept of context switching.

Now that we are through with the client-server architecture, let’s jump on to our main topic – What is Nodejs?

What is Nodejs?

Nodejs is basically an open-source runtime environment. It uses Javascript to execute server-side as well as networking applications. Nodejs works on an event driven architecture. It is built on top of Google Chrome’s Javascript V8 engine. It compiles the javascript code natively into machine code with the help of V8 compiler. The library of Javascript executes the code efficiently and faster.

We have already discussed the even driven architecture (single-threaded) and its advantages. The same set of features and advantages are applied to Nodejs as well.

Nodejs is asynchronous, it has a –

  • recipient (that receives the requests),
  • a caller (that sends the requests)
  • and also, a call back function.

When a request is made to the server by the caller, it is received and processed but meanwhile server continues to process other requests and doesn’t wait for the previous ones to get executed. This means that no request made to the server will ever be blocked. This mechanism makes Nodejs highly scalable, efficient and fast.

We would now begin with the practical implementation of Nodejs. Here, we are skipping with the installation and other basic steps to be executed before writing the first Nodejs program.

We will start with the basic theory of Nodejs that is – Data types in Nodejs,

What are Data Types in Nodejs?

The data types in Nodejs are similar to those in most computing languages like C++, Java etc.

In Nodejs, there are two kinds of data types – Primitive and abstract. The primitive data type contains –

  • String
  • Number
  • Boolean
  • Null
  • Undefined.

Abstract data type contains Array, date and many more. There are various abstract data types in Nodejs. In Javascript, you don’t have to declare the data type used.

Let’s begin with looking at the syntax of using String in Nodejs in your program. Here’s the code –

  • var name = “Konfinity – A place to upskill your career”;
  • console.log(name);

The output of the code above would be “Konfinity – A place to upskill your career” printed on your console. (The output would be without the inverted quotes.)

After data types, the next in line is functions. A JavaScript function allows you to define a block of code, give it a name and then execute it as many times as you want. A JavaScript function can be defined using the function keyword. For a detailed analysis, you may refer to this blog post.

Now, if you have a fair idea of what is Nodejs and the landscape it fits, we would proceed further and understand the technicalities and details of Nodejs starting with Nodejs modules.

What are Nodejs Modules?

Modules are basically libraries, a set of collection you want to include in your application. In order to include a module in your program, the require () function is called.

Nodejs consists of various modules, these modules are divided into three categories namely – core, local and third-party modules.

  • Core Modules

    The Core Modules are available in the default installation of Nodejs. No separate download or installation is required in case of core modules. They are also called built-in modules. Some examples of core modules are http, url, path etc.

  • Local Modules

    The local modules are built and exported by the programmer. They are not available anywhere else (neither in the default installation nor in external dependencies). They are also called custom modules. Local modules can be functions, objects etc.

  • Third-Party Modules

    The third-party modules are the ones those have to be installed via external sources. Some examples of third-party modules are React, Angular, Bootstrap etc. In Nodejs the third-party modules are available in the NPM repository. The NPM repository is explained below.

We have talked about the Nodejs architecture and now it’s time we discuss the Node Package Manager (NPM). A package manager for all Nodejs packages, NPM is now a default in Nodejs installation.

NPM has online repositories of Nodejs packages/modules which are supposed to be used in Nodejs projects. It also provides CLI (Command line Interface) utility in order to install various Nodejs packages and manage their versions and dependencies.

Now we are going to discuss some prominent modules in Nodejs

Using Nodejs modules

File system is popularly known as the fs module in Nodejs. It is used to read, create, update, delete and rename files. The fs module is a core model in Nodejs that means there is no need to install or download any external dependency. We would be now writing a code where the module file system would be used to read a file by the name of konfinity.txt.

  • const fs = require (‘ fs ’);
  • // The require () function is needed to use any module in Nodejs.
  • fs.readfile(‘./konfinity.txt’, function (err, data)(
  • if (! err)
  • {
  • console.log (data);
  • }
  • )

The fs.readfile uses the call back function for its execution. The call back function uses data and error as arguments. According to the code, if there is no error then the contents of the file konfinity.txt would be printed on the console.

One of the third-party modules in Node.js is Express. Express is basically a web framework for Nodejs. This framework allows you to make single, multiple and hybrid web pages in a more efficient manner. The syntax for the same is –

  1. var express = require(‘express’);
  2. var app = express ();

For a more detailed analysis, you can go through this blog post.

Now we will discuss about another important feature in Nodejs, JSON

What is JSON in Nodejs?

Now we will discuss about another important feature in Nodejs, JSON. It is an acronym for Javascript Object Notation. JSON is basically a lightweight data format. It is extensively used in software and especially in applications that are based in Javascript.

Json is the heart of any Nodejs Project. It is actually a Manifest file which contains the metadata of the project. The package.json file is inside the task manager.

It contains every information of the project like name, version and authors of the project. It can also include the name of third-party modules, tools and scripts used and an array of other useful and relevant information.

The MEAN Stack

The MEAN Stack refers to a collection of Javascript technologies that are used to build web applications.

  • Nodejs is a part of the MEAN stack. This stack consists of Angular for displaying the end result to the user (basically the front end).
  • Mongo Db database for storing and retrieving data.
  • Node JS for handling client and server requests
  • And of course, Express for making the request to the database and returning a response.

The step by step procedure of how request is received and processed is given below -

  1. In the first step, the client makes a request which goes to the Angularjs.
  2. The request is then parsed by the Node JS server.
  3. The expressJs makes a request to the database which in this case is MongoDB.
  4. The database then returns the request.
  5. AngularJs displays the output as the final step.

The entire MEAN stack is illustrated through the graphic.

Nodejs in MEAN Stack.

We hope that the question “what is Nodejs?” is answered. However, if there are still some doubts regarding the topics covered, you can ask us anytime and we would be more than happy to answer.

The popularity and relevance of any technology is determined by the companies using it at present. In recent years, Nodejs has become one of the most trending technologies to be used in the world of web development.

Giant cooperatives like Netflix, Linkedln, Uber and many others are using Nodejs for handling their day-to-day functions on the web. Nodejs is a part of the stack of technologies that helps you with accessing the best of technology. Be it finding the nearest car for you or providing you with the best streaming experiences or helping you grow your professional network.

If you are passionate about the world of technology then Nodejs should definitely find a place at your desk. In this article we have tried to cover the concept of Nodejs comprehensively but in order to make a lucrative career for yourself, you need professional training under the guidance of able mentors.

In case you didn’t know, Konfinity ‘s Web Development Course is developed by experts from IIT DELHI in collaboration with tech companies like Google, Amazon and Microsoft. It is trusted by students and graduates from IIT, DTU, NIT, Amity, DU and more.

Join the course to master the art of creating web applications by learning the latest technologies, right from basic HTML to advanced and dynamic websites, in just a span of a few months.

Konfinity is a great platform launching a lucrative tech career. We will get you started by helping you get placed in a high paying job. You know what’s amazing - no prior coding experience is required to take up our courses. Start your free trial here.