DEV Community

Daxesh Italiya
Daxesh Italiya

Posted on

How do I prevent the node.js server from crashing on uncaught exception

As a good developer on uncaught exception, we need two things

1. Our Server should not crash
2. Need detailed root cause of the exception as a Log for improvement in code

To achieve both points I would recommend you to Add Wooffer (https://wooffer.io/) in your Code. you will solve this crashing problem and get more than your expectation

Step to achieve feature

Step 1: Install

npm i wooffer

or

yarn add wooffer

Step 2: Setup ENV

token = "<Your Token>";
serviceToken = "<Your Service Token>";
Enter fullscreen mode Exit fullscreen mode

Note: get your token and Service Token from https://app.wooffer.io/

Step 3: Add Wooffer to the Root directory like index.js or app.js

const wooffer = require("wooffer");
const express = require("express");

wooffer(process.env.woofferToken, process.env.woofferServiceToken);

// Add this code to track endpoint usage(Optional)
const app = express();
app.use(wooffer.requestMonitoring);
Enter fullscreen mode Exit fullscreen mode

Once This process is done now your node server no more crash on uncaught exceptions and provide real-time slack alert

Top comments (0)