Now, before getting into learning the nitty-gritty of Node.js, let us first understand why it is advisable to use Node.js.
Why use Node.js?
There are several reasons why you would want to use Node.js. Some of the benefits of using Node.js are as follows:
- It has strong technology support
- Better and faster processing because of event-based modeling
- Node Package Manager (NPM)
- Widely accepted
1. It has strong technology support
2. Better and faster processing because of event-based modeling
Node.js has event-based modeling. Anything and everything is done only if an event occurs. To every event, a callback function is associated which has the task to be done upon the occurrence of such an event. The asynchronous behavior helps in processing these callback functions fast.
You can also use an Event Emitter to explicitly fire an event to which anyone can listen. An event can be emitted multiple times and you can configure your application to listen to it only the first time and not call the callback function the other times the event is triggered.
Node.js is a very light-weight technology and so, it can be used to have micro-service architecture. IT is more suitable to have a single application that houses multiple microservices, each for a small task to have code reusability and flexibility. Node.js is a perfect choice for this kind of architecture. This makes it very easy to integrate the updates. The new micro-services can be added to the already deployed application very easily without requiring many changes to be done to the original code. So, it is not compulsory to have a monolithic architecture. Having micro-services also improves the response time of the application. Also, a large number of requests can be served concurrently without having a hardware downtime. There are several use-cases that prove this.
4. Node Package Manager (NPM)
5. Widely accepted
Node.js is very popular in the industries. Many organizations are using, and also promoting the use of Node.js for web development. This increases the chances of your open-source project getting help from the leading companies across the globe.
These are the most commonly know benefits of Node.js. But, the actual list is very long. Now that you have a basic idea about the benefits of Node.js, you might be interested in using it in practice.
The following passage is a guide for beginners of Node.js. It will walk you through the installation process, sample application, how to use NPM, and much more.
How to install Node.js?
For Windows users, go to Node.js for Windows to download the package, while Linux users can directly use the installer. You should check the GitHub repository to see if there are any updates available from time to time.
Once you are done with downloading and installing Node.js, you can test the installation by running the command ‘node -v’ on your command prompt. In case of any kind of error you might want to refer to this link.
In the similar way you can also download and install NPM – Node Package Manager. NPM can be used for downloading any further updates in Node.js as well as other packages that you would want to use in your applications.
Now that you are done with installing Node.js, let us see how to build an application. For demonstration let us create a ‘Hello World!’ application.
Steps to create a Node.js application – Hello World Application
- Open the terminal if on Linux, and the Command Prompt if on Windows. Change your current directory to the location where you want to create your application. Create a folder named ‘hello_world’
- Change your current directory to the project’s directory – the one we just created. In this folder, create a file named app.js.
- Now, paste the following content in the app.js file that we just created.
- By doing so you created an HTTP Server that you will need to run the application. The argument to the create server function is the callback function that is to be called upon creation of Server. In this function, we are printing plain text – ‘Hello World!’ This is added to the response variable that will be returned. The listen method specifies the port number on which the server has to listen to the requests of this application. For any HTTP server, the default port number is 80, but it is advisable to explicitly specify a different port number.
- Now that we have written the code for our application, we are only left to run the application and check the output. IN order to run the application, use the following command:
- Now, to see the output, go to the port you have specified, here 4000.
Go to the browser and type ‘http://localhost:4000/’in the URL space and hit Enter to see the output. You will see ‘Hello World!’ in the browser window.
Hurray! You now have a server up and running. Host create your application and do wonders with Node.js
What is NPM?
NPM stands for Node Package Manager. As its name suggests, it manages the packages of Node. It has a great number of packages/libraries that can be used in Node.js. These packages can be installed with NPM and used.
Using the –save option saves the package in your local system so that it can be used in the future without having to download it again.
The first and foremost command that you should run is ‘npm install’. It will look for all the packages listed in your package.json file and install them automatically, saving you the trouble.
How to use packages?
By using the ‘require’ function, you can include the package in your code and store it in a variable that will let you use it.
const http_package = require(‘https’)
Using require, we can load modules that come by default with Node.js, the third-party packages that we have installed for our use, as well as our local files. Thus, it creates a room for fine-grained applications. In fine-grained applications, we can create micro-services and integrate them using ‘require’.
Node.js can be used with many frameworks that make it easier to create and use a web server. Express Framework is one of the most widely used frameworks for Node.js applications. Express is widely used as one of the components of MEAN (MongoDB, Express, AngularJS, Node.js) stack web applications.
Node.js can be used for many real-time applications such as Chat Application, Proxy, Data Streaming, Brokerage – Stock Traders’ Dashboard, Other Monitoring Dashboards, etc. In short, Node.js can be used for most of the server-side web applications. But, if the server uses a Relational Database than Node.js is not a wise choice as it hinders the Non-Blocking I/O of Node.js and thereby refrains concurrent request processing. Also, if the computations on the server-side are very heavy, then Node.js is not a wise choice as Node applications are driven by a single thread and single core of CPU; this decreases the speed of computation and increases processing time. After all, Node.js was never created to solve computation problems. It was created to solve I/O problems. So, use Node.js if your application has more importance of the response time than computation and get the benefit of Node’s scalability to get fast non-blocking I/O.