Introduction to NodeJS

What is NodeJS?: Node JS is an open-source and provides a cross-platform runtime environment that helps for executing javascript code outside the browser. Hence it is not the framework and its not a programming language. Most of the time node js are used as building back end services like APIs (Web App or Mobile App). Large companies like Netflix, Wallmart, PayPal, Uber use the node js.

Installation of NodeJS

With Website:

1. You can download the node js from the official website https://nodejs.org/en/ and click on the LTS version.

2. Check the version of your node js, so type the following command in the command prompt.



~$node --version

3. Now you have to create a folder and add some files like example.js.To run this file type the following command in command prompt.

first app $node example.js

4. The node js provides some build-in modules that help to develop your applications. A few of those modules are HTTP, URL, events, OS, fs and these can be added as the following command.

vas fs=require('fs');

5. Let’s see an example that includes an HTTP module that builds servers.

var http = require('http');

Sample Example

// Create a server object:
http.createServer(function (req, res) {
// Write a response to the client
res.write('tutorials.freshersnow');
// End the response
res.end();
// The server object listens on port 8080
}).listen(8080);

Once you write and run the file in command prompt then it executes the file and listens to the server on the 8080 port. Hence you can develop your own module and you can include it in your file.

With NPM:

NPM (Node Package Manager) provides packages that contain files and modules. These files and modules are required for your application which can be download and use them easily.

To install the package you need to type the following code

npm install

Event module Example

var events = require('events');
var eventEmitter = new events.EventEmitter();
// Create an event handler:
var myEventHandler = function () {
console.log('Welcome to Tutorials.freshersnow');
}
// Assign the event handler to an event:
eventEmitter.on('tutorial', myEventHandler);
// Fire the 'tutorial' event:
eventEmitter.emit('tutorial');

Hence now you can use node js and build your own applications. Some of the frameworks of node js Express.js,Sail.js,Socket.io,Meteor.js,Koa.js.

Features of NodeJS

There are many programming languages to build back end services but why node js?. The following reasons will answer your question.

  • Node JS provides fast and highly scalable services.
  • The node js uses javascript so the javascript programmer can easily build back end services.
  • It is easy to start and used for agile and prototype development.
  • The source code in node js is more consistent and clean.
  • It provides a large ecosystem for open source libraries.
  • Node JS provides non-blocking nature.

Advantages of NodeJS

Easy to learn and code

The Node Js is very easy to learn and code because of javascript. If front end developer has good knowledge of javascript then it’s very easy to build an application on Node JS.

Easy Scalability

Most of the developers use Node JS because they easily scale the application in horizontal and vertical directions. If required you can add extra resources during the scalability of the application.

Fast Suite

The Node Js runs on the V8 engine which is developed by Google. Node JS acts as a fast suite because event loops in node js handle asynchronous operations. All the operations in the node js are executed quickly like network connection or file system, reading or writing in the database.

Advantages of Caching

Node Js provides caching of a single module. If there is a request for the first module, the node js gets cached in the application memory which doesn’t require re-execution of the code.

Real-time Web App

To develop any web app, PHP is used but the same amount of time is required if you use node js. In case you are creating chat apps or gaming apps the node js is the best choice because of fast synchronization and avoids HTTP overload in the event loop.

Hosting

The hosting platform of node js is Platform as a Service (PaaS) and Heroku that are very easy to use without any issues.

Data Streaming

In Node, JS HTTP requests and responses are two separate data stream events. In case you process the file it reduces the overall time and makes the fast transmission. Node JS allows streaming audio and video files at lightning speed.

Corporate Support

Most of the companies like Wallmart, Microsoft, PayPal, Yahoo uses Node JS to build applications. The Front end and backend teams are combined together as a single unit in many companies because node js uses javascript.

NodeJS Applications

  • Complex single-page applications
  • Real-Time Chats
  • Real-time collaboration tools
  • JSON APIs based application
  • Streaming apps