Introduction to Nodejs
Tags: webdev, tutorial, node, beginners
Published: 2022-09-09 12:11:22


## Introduction

Node.js is an open-source and cross-platform runtime environment for executing [JavaScript](https://www.geeksforgeeks.org/JavaScript-tutorial/) code outside a browser. You need to remember that **NodeJS is not a framework and it’s not a programming language**. Most people are confused and understand it’s a framework or a programming language. We often use Node.js for building back-end services like APIs, Web Apps, or Mobile Apps. It’s used in production by companies such as Paypal, Uber, Netflix, Walmart, and many others.

![Photo by [Juanjo Jaramillo](https://unsplash.com/es/@juanjodev02?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/10368/0*AftLQTZNUCihgVoN)

## Features of Node.js:

There are other programming languages also that can be used to build back-end services so what makes Node.js different?

 1. It’s easy to get started and can be used for prototyping and agile development

 2. It provides fast and highly scalable services

 3. It uses JavaScript under the hood, so it’s easy for a JavaScript programmer to build back-end services using Node.js

 4. Source code is cleaner and consistent.

 5. Large ecosystem for open source library.

 6. It is Asynchronous or Non-blocking in nature.

## Advantages of Node.js:

Here are the benefits of using Node.js

 1. **Easy Scalability:** Developers prefer to use Node.js because it is easily scalable. We can also add extra resources during the scalability of the application.

 2. **Real-time web apps:** If you are building a web app you can also use PHP, and it will take the same amount of time as you use Node.js, But if I am talking about building chat apps or gaming apps Node.js is much more preferable because of faster synchronization. Also, the event loop avoids HTTP overloading for Node.js development.

 3. **Fast Suite:** NodeJs runs on the V8 engine developed by Google. The event loop in NodeJs handles all asynchronous operations so NodeJs acts like a fast suite and all the operations can be done quickly like reading or writing in the database, network connection, or file system

 4. **Easy to learn and code:** NodeJs is easy to learn and code because it uses JavaScript. If you are a front-end developer and have a good grasp of JavaScript you can easily learn and build the application on NodeJS

 5. **Advantage of Caching: **It provides the caching of a single module. Whenever there is any request for the first module, it gets cached in the application memory, so you don’t need to re-execute the code.

 6. **Data Streaming:** In NodeJs HTTP request and response are considered as two separate events. They are data streams so when you process a file at the time of loading it will reduce the overall time and will make it faster when the data is presented in the form of transmissions. It also allows you to stream audio and video files at lightning speed.

 7. **Hosting:** PaaS (Platform as a Service) and Heroku are the hosting platforms for NodeJS application deployment which is easy to use without facing any issue.

 8. **Corporate Support:** Most of the well-known companies like Walmart, Paypal, Microsoft, Yahoo are using NodeJS for building the applications.

## Application of NodeJS:

NodeJS should be preferred to build:

* Real-Time Chats,

* Complex Single-Page applications,

* Real-time collaboration tools,

* Streaming apps

* JSON APIs based application

## Installing Node and using It:

* **Using Website:**

1. You can visit the link [Download Node](https://nodejs.org/en/) and download LTS version.

2. After installing the node you can check your node version in the command prompt using command..

    ~ $node --version

3. After that, you can just create a folder and add a file here for example app.js. To run this file you need to execute the command…

    first app $node app.js

**4. Node Modules:** There are some built-in modules that you can use to create your applications. Some popular modules are- OS, fs, events, HTTP, and URL, and then you can include these modules in your file using these lines.

    var fs = require('fs');

5. Here is an example of how to include an HTTP module to build the server…

javascript

    var http = require('http');

    // Create a server object:
    http.createServer(function (req, res) {

        // Write a response to the client
        res.write('Introdution to Nodejs'); 

        // End the response  
        res.end(); 

    // The server object listens on port 8080
    }).listen(8080);

This will listen to the server on port 8080. Once you will run your file in the command prompt it will execute your file and listen to the server on this port. You can also create your own module and include it in your file.

* **Using NPM: **NPM is a **Node Package Manager** that provides packages to download and use. It contains all the files and modules that you require in your application. To install any package you need to execute a command…

    npm install

This is an example of using the Events module.

javascript

    var events = require('events');
    var eventEmitter = new events.EventEmitter();

    // Create an event handler:
    var myEventHandler = function () {
      
        console.log('Welcome to My Blog');
    }

    // Assign the event handler to an event:
    eventEmitter.on('savvy', myEventHandler);

    // Fire the 'savvy' event:
    eventEmitter.emit('savvy');

So this is how you can start with Node.js and build your own applications. There are some frameworks of the node which you can use to build your applications. Some popular frameworks of node are…**Express.js, Socket.io, Koa.js, Meteor.js, Sail.js**.