Tools to speed up node.js application development

There are some tools in Node.js which are absolutely necessary to speed up the development process. Till now, we have been using npm start command to start our application and every time we make some changes in our project, we need to restart the server manually and that causes massive delays in terms of delivery of the project. Also, we have been using CURL command to test apis which is extremely powerful but in terms of ease of use, tools with graphical user interface is much better and user friendly.

There are many tools developed for Node.js to speed up the development process and few of them are listed below:

1. nodemon

nodemon is one of the most important tool which helps us by automatically restarting the application server when file changes are detected in our project directory. The best part about this tool is, it doesn't require any changes to our code. Since nodemon is a wrapper around the node command, we can easily replace node with nodemon.

Let's install the nodemon in our local system. Most people prefer to install it as a global package so that they can directly run the nodemon command in their terminal/command prompts. To install nodemon globally on our machine, run the following command:

   
   	npm install -g nodemon
   

If you face issues with permission denial for global installation, run the command with sudo access. This is not recommended and there is a better way to solve the permission issue. We will discuss about solving permission issue later.

   
   	sudo npm install -g nodemon
   

Let's verify the nodemon installation by checking its version.

   
   	nodemon -v
   

If it gives the version number, the installation is all good and we can start using nodemon command.

Nodemon version check

From root of the project directory, issue the following command to run the server:

   
   	nodemon index.js
   

Here index.js file is the main entry point to our travel application. Make some changes in some files in the project directory, you will notice - the server is automatically restarted and changes are reflected.

Nodemon version check

We can also install nodemon as a project dependency for which we need to issue the following command:

   
   	npm i nodemon
   

Once the package is installed, make some changes in the scripts property of package.json file in the following way:

   
	"scripts": {
    	"test": "npm run test",
	    "start": "node index.js",
	    "start:dev": "nodemon index.js"
	  },
   

We have added the start:dev command in the scripts property and to run the server using nodemon, we need to issue the following command:

   
   	npm run start:dev
   

Both global dependency and project dependency works and we can use either of them to start our server in development environment.

Sometimes, we do not want nodemon to automatically restart the server for certain files/folders and in those cases, we need to put them in the nodemon ignore lists. Suppose we don't want nodemon to watch node_modules directory, to achieve this we can issue the following command:

   
   	nodemon index.js --ignore node_modules/
   

The better way to implement these kinds of things is using nodemon configuration file called nodemon.json which should be located at the root of the project directory. It looks like below:

   
	{
	    "restartable": "rs",
	    "ignore": [
	      ".git",
	      "node_modules/**",
	      "*.test.js",
	      "*.spec.js"
	    ],
	    "verbose": true,
	    "watch": [
	    ],
	    "env": {
	      "NODE_ENV": "development"
	    },
	    "ext": "js,json"
	  }
   

2. PM2

PM2 is one of the most popular daemon process manager for Node.js which helps us to monitor, manage and keep the application process alive in production servers. A process is a program in execution and a process manager is a system which helps in creating, scheduling, monitoring and termination of processes. So, it basically monitors the application and in case of any crashes, it will restart the application automatically and also exposes different useful metrics for monitoring purpose.

Let's install the PM2 globally:

   
   	npm install pm2@latest -g
   

Once installed, check the version of PM2 installed in the system:

PM2 version check

To start the application with PM2, let's issue the following command:

   
   	pm2 start index.js
   
PM2 start application

So, from the image, we can see it has an id of 0 and name of index , both of which uniquely identifies this travel application server. So, the name is auto assigned and it's best to give the name manually when starting the application with pm2 command.

First, let's check the logs using the name:

   
   	pm2 logs index
   

We can also check logs by id as well

   
   	pm2 logs 0
   
PM2 logs

If you want to list all the applications started using pm2 command, then issue the following command:

   
   	pm2 list
   

Now, before starting the server again with custom name, we need to remove the existing one, else it will spin up another instance of the same application.

   
   	pm2 delete 0
   

Now, let's start the server with custom name using pm2 command:

   
   	pm2 start index.js --name travel-app
   

Also, we can configure PM2 to automatically restart all the applications even on server boot so that there is no downtime. Issue the following command to generate an active startup script:

   
   	pm2 startup
   
PM2 startup to start the application on server reboot

In the above image, you can see, when we issue the pm2 startup command, it asks us to run a command as shown in the image starting with sudo keyword for setting up the startup script. Copy the command as shown in your terminal and run it. For me, it looks like below:

   
   	sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup launchd -u shrawanlakhe --hp /Users/shrawanlakhe
   
PM2 startup activation

Now, let's run a following command to freeze a process list for automatic respawn.

   
   	pm2 save
   

PM2 also provides us a real-time monitoring of its processes containing informations such as metrics, logs and various other metadata.

   
   	pm2 monit
   
PM2 Monitoring

Couple of other helpful PM2 commands:

   
   	pm2 restart app_name
	pm2 reload app_name
	pm2 stop app_name
	pm2 delete app_name

   

Just like with nodemon, we can achieve automatic restarting of the application based on the file system changes using pm2. We can also ignore certain files/folders from the watch list.

   
   	pm2 start index.js --watch
   

PM2 also uses a JSON/YAML configuration file to manage the application. And using configuration file is the recommended approach. Create a file called process.json at the root of the project directory.

   
	{
	    "apps": [{
	        "name" : "travel-application",
	        "script" : "index.js",
	        "watch": true,
	        "watch_delay": 1000,
	        "exp_backoff_restart_delay": 100,
	        "restart_delay": 3000,
	        "max_memory_restart": "300M",
	        "ignore_watch": ["node_modules", "package.json", "package-lock.json"],
	        "env" : {
	            "PORT" : 3000
	        },
	        "env_production" : {
	            "PORT" : 3000
	        }
	    }]
	}
   

To start the application server with PM2 process.json file, issue the following command:

   
   	pm2 start process.json
   

If there is new additions to the PM2 applications or modifications in the existing PM2 configuration file, then we need to again run following command to freeze the updated process list for automatic respawn. For existing one, We need to reload first.

   
   	pm2 save
   

3. Postman

Postman is one of the most popular API testing tool. It simplifies each step of the API lifecycle and streamlines collaboration so that we can create better APIs faster. It's graphical user interface along with other extensive features make the API testing very smooth. Postman supports a wide range of API standards, specs and formats, including REST, SOAP, GraphQL, OpenAPI, RAML, Swagger, JSON, cURL, WADL and more.

Let's install postman by navigating to the following link:
https://www.postman.com/downloads/

Download Postman

The postman downloads page will show the download option as according to your machine operating system. The above image shows options to download for mac machines. Download and install the postman in your system. Once, postman is installed, it looks like below:

Postman UI

This is the user interface for our travel application workspace. To create a workspace, click on the Workspaces menu at the top left corner and then click on Create Workspace button.

postman workspace uipostman workspace ui

Now that we have created a workspace, let's create a collection for hotel module. You can click either on plus (+) icon or Create collection button from the image below to create a collection.

postman create collection

Once hotel collection is created, let's create a GET request for fetching the list of hotels:

postman create request

In the above image, you can see Add request, click on that menu.

postman get request

Click on Send button to get the list of hotels:

postman get request with response

This is how easy it is to test api endpoints with Postman. This is all for this chapter.

In our next chapter, we will discuss about git and create a github repository for our travel application project.

Prev Chapter                                                                                          Next Chapter