I have a simple express app with handler (just for demonstration it's just sending error with status code 400):
router.post('/createUser', cors() as RequestHandler, async (req, res, next) => {
return res.status(400).json('Something went wrong');
});
I also tried res.status(400).send('Something went wrong');
, but it's being sent as HTML.
Tried res.status(400).send(new Error('Something went wrong'));
, it's not being caught, and the response is {}
.
When I call the endpoint:
fetch(`${url}/createUser`)
.then(res => {
// this block gets invoked
}).catch(e => {
// instead of this
})
How to send the response with express, so the above issue gets solved?