nodejs how to send error response automatically if anything fails

Issue

This Content is from Stack Overflow. Question asked by Sunil Garg

In Nodejs, for every api i am using try/catch block, if everything works i return the response with 200 and in catch block i need to use the res.status(500).send()

is there any way i can avoid using try/catch like if anything fails, some middleware or anything can catch the error and send the error response to user.

Like in NestJS you just simply use the return response, no need to even use res.send to send the response, if there is error it automatically send the 500 response



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?