DEV Community

Emon Islam
Emon Islam

Posted on • Edited on

A graceful error handle for Express applications

A graceful error handle for Express applications. This also patches a DOS exploit where users can manually trigger bad request errors that shut down your app.

package: [express error handle]

Installation

(https://www.npmjs.com/package/express-error-handle)
npm i --save express-error-handle

Express express error handle

const express = require('express');
const {errorLog,errorHandlerNotify} = require("express-error-handle")
const app = express()
const port = process.env.PORT || 5000;

app.get('/a-typical-route',async function(req, res, next) {
try{

//your code here

}
catch(error){
next(error)
}

});


app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})

//handling error using at the end of last routes
app.use(errorLog);
app.use(errorHandlerNotify);


Enter fullscreen mode Exit fullscreen mode

Supported environment variables

create root path .env file and then put here

open project folder and create

touch .env

only use developemnt purpose

NODE_ENV=development

production serve using applications

only use production purpose

NODE_ENV=production

Image of Bright Data

Overcome Captchas with Ease – Keep your data flow uninterrupted.

Our Web Unlocker smoothly handles captchas, ensuring your data scraping activities remain productive.

Solve Captchas

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay