Programming

Nodejs: An Introduction and its Uses

Node.js is a server-side JavaScript platform that uses an event-driven, non-blocking I/O model. Its design philosophy is to provide a single programming interface for both web applications and standalone JavaScript programs.

Node.js was created by Ryan Dahl in 2009, and its development and maintenance are currently overseen by Joyent. The main purpose of Node.js was to build fast and scalable network applications. It is also an event-driven, efficient, non-blocking I/O model and a perfect real-time application that is capable of running on many distributed computers.

Node.js is platform-independent, which means, it can run on any platform like Linux, Windows, etc. It is an open-source application that can run on a cross-platform run-time environment for server-side and networking applications. It is made up of a large library with many JavaScript modules that can be used in web applications and to make them more flexible.

In 2014, Node.js became the world’s first JavaScript runtime to support the V8 JavaScript engine, which is a well-known open-source engine. The JavaScript engines in Node.js are well suited to handling real-time and non-real-time, asynchronous, and event-driven applications and web services. Node.js supports both server-side and client-side code, while using JavaScript as the core language. Because JavaScript is a platform-independent language, Node.js allows its applications to run across multiple operating systems and hardware platforms.

Why Node.js and not any other?

Well, when it comes to the dynamic handling of web pages, then Node.js is more powerful. You can say that PHP and ASP.net can handle the dynamic rendering of pages, but the problem comes with asynchronous web page handling.

In simple words, when a PHP or asp.net server send’s a web page to the browser, then it waits for the receiver to open and read the file and then process another request. So it takes a little time in this process. While in Node.js, as soon as the server sends the task, the Server is ready to handle the next response. Another major reason you should be using Node.js is that it’s single-threaded, non-blocking, asynchronously programming and memory efficient.

Features in Node.js

Node.js is designed with real-time applications in mind so that the whole thing is more efficient. The concept of asynchrony was brought from event-driven programming using web browsers and later moved to other systems like command line tools. The asynchrony concept has become very popular among programmers who work with node.js, and they are the ones who appreciate its real-time features. Some other key features of Node.js include:

  • It can create, open, close, read, write and delete files on the server easily.
  • It can add, modify and delete data in your database.
  • It can generate the dynamic web page content.
  • It uses JavaScript on the server.
  • Node.js can run on various platforms; it’s free and has an open source server environment.

Advantages of using Node.js

  • Node.js removes the waiting time and continues to process the next request.
  • It runs on single-threaded, non-blocking, asynchronously programming, which is very memory efficient.
  • Node.js is designed to never buffer any data. The application have large output in data.
  • With Google’s V8 JavaScript Engine, the Node.js library is very fast and easy to handle.

Disadvantage of Node.js

Node.js is not suitable for CPU intensive or CPU intensive like applications.

Download and install Node.js for your computer

The source code is written in javascript and we will use an interpreter for executing our Node.js javascript code. You can download it from the official link. It is also recommended to use Visual Studio Code so that you can see and understand the work that goes into running code.

Verifying Installation

After downloading and installing it, you can check it on your system. Simply create and save a file with sample.js name and save. Type the below code on Visual Studio Code Terminal:

Console.log(“Hello and Welcome to DigitalGyan”)

It’s not compulsory to use ; (semi-colon) at the end. But as a programmer, you should use it.

After that type:  

node sample.js

If everything is working, then you will get the Output

Hello and Welcome to DigitalGyan

Creating a Simple Web Server on your local machine

To create a simple web page, follow the steps below:

Create a file with name sample.js and save it

Write the following code:

Var http = require(‘http’);

# require(): This method is used to add a header file, which consist of pre-defined functions.

http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’ : ‘text/html’ });
res.end(‘Hello World!’);

}).listen(8080);
# the createServer function is used to create a Server that will handle request and response method to your local browser.

#res.write is used to generate a response status with 200, “OK” and the type of file which server will send.
# res.end is the output that will be returned.

We’ll cover more about the code in the later course.

Save the file and open the terminal and type

node sample.js

Now, your computer becomes a server.

After this, you can open your browser and type the following address

https://localhost:8080

you will get the output.

nodejs example

If anyone tries to connect with this port 8080 to your computer. This will display the same output message.

Is JavaScript necessary for Node.js?

Node.js was originally built on the purpose of extending the features of Java programming. But at the same time, it must handle the request and response, which can be in XML or other languages. Hence, a newer version of ECMA script was written. And later that it was converted to Javascript programming. Therefore, Javascript is given more focus for Web Development in Node.js .

Which Big Companies Uses Nodejs?

NodeJS is not only used by large tech companies; it is also used by many small businesses and start-ups. Some of the Big Companies that rely on Nodejs are eBay, GoDaddy, General Electric, Microsoft, PayPal, Uber, Wikipins, Yahoo, Netflix, etc.

When to use Node.js?

If you have these conditions, then Node.js is the best tool you should use. These conditions are:

  • Data Intensive Real-time Applications (DIRT)
  • Single Page Applications
  • I/O bound Applications
  • JSON APIs based Applications
  • Data Streaming Applications

It’s actually possible to build a fully featured, high-performance business application using Node.js without ever requiring the support of an operating system. Sure, there are other ways to build an application. But Node.js is definitely one of the better ways to do it.

Show More

Related Articles

Leave a Reply

Back to top button

Adblock Detected

Please turn off Ad Blockers to continue.