Node JS Convert 404 API Route to Proper Message

We Generally Face A Problem of Bad Error Handling If Route Not Found in Node JS, Let's See How Can We Fix This


Node JS Error Handling
Error Handling in Node JS


Via adding below 4 lines code in our main node js file, we can manage it easily:


//404 error handling
app.use((req,res) => {
const error = new Error("Not found.")
req.code = 1
req.statusCode = 404
ResponseMiddleware(req, res, error.message)
})



That's it !

Happy Coding !

Comments

Popular posts from this blog

JavaScript Logical Output Based Interview Questions

Create and Deploy AWS SAM Application

Deploy Angular Build With Express JS Project