How to restart Node.js application when uncaught exception happen ?
Last Updated :
07 Aug, 2024
In this article, we are going to learn, about restarting a Node.js application when an uncaught exception happens. For this, we are going to use the pm2 module.
Approach: Let's see the approach step by step:
- Step 1: Install the pm2 module and use it to start the server.
- Step 2: When an uncaught exception happens, then execute the command process.exit() to stop the server.
- Step 3: Then, pm2 module will automatically start the server again.
process.exit() stop the server and pm2 force it to start. In this way, the server will restart.
Implementation: Below is the step-by-step implementation of the above approach.
Step 1: Initializes NPM: Create and Locate your project folder in the terminal & type the command
npm init -y
It initializes our node application & makes a package.json file.
Step 2: Install Dependencies: Locate your root project directory into the terminal and type the command
npm install express pm2
To install express and pm2 as dependencies inside your project
Step 3: Creating a list of products: Let's create an array of products and set it to constant products.
const products = [];
Step 4: Creating Routes for the home page and the products page: Let's create two routes so that users can access the home page and the products page.
app.get('/', (req, res) => {
res.send('Hello Geeks!');
});
app.get('/products', (req, res) => {
if (products.length === 0) {
res.send('No products found!');
process.exit();
} else {
res.json(products);
}
});
Inside the product route, we use process.exit() method to stop the server.
Complete Code:
JavaScript
const express = require('express');
const app = express();
const products = [];
app.get('/', (req, res) => {
res.send('Hello Geeks!');
});
app.get('/products', (req, res) => {
if (products.length === 0) {
res.send('No products found!');
process.exit();
} else {
res.json(products);
}
});
app.listen(3000, ()=>{
console.log('listening on port 3000');
});
Steps to run the application: Inside the terminal type the command to run your script 'app.js' with pm2.
pm2 start app.js
Output:
Similar Reads
How to run a node.js application permanently ? NodeJS is a runtime environment on a V8 engine for executing JavaScript code with some additional functionality that allows the development of fast and scalable web applications but we can't run the Node.js application locally after closing the terminal or Application, to run the nodeJS application
2 min read
How can We Run an External Process with Node.js ? Running external processes from within a Node.js application is a common task that allows you to execute system commands, scripts, or other applications. Node.js provides several built-in modules for this purpose, including child_process, which allows you to spawn, fork, and execute processes. This
4 min read
Unit Testing of Node.js Application Node.js is a widely used javascript library based on Chrome's V8 JavaScript engine for developing server-side applications in web development. Unit Testing is a software testing method where individual units/components are tested in isolation. A unit can be described as the smallest testable part of
5 min read
How to set up your Node.js and Express development environment? In this article, we are going to learn how to set up your NODE JS and Express JS development environments. Node JS and Express JS development environments are used to write the code for the server side. Server-side code is responsible for handling HTTP requests and API endpoints of your application.
4 min read
How to Handle Lost Connection to Mongodb from Nodejs? Handling lost connections to MongoDB in a Node.js application is crucial for maintaining application reliability and data integrity. However, network issues, database server crashes, or other unexpected events can cause the connection to be lost. This article will guide you through different approac
3 min read