Categories
JavaScript Answers

How to specify HTTP error code using Express.js?

Spread the love

Sometimes, we want to specify HTTP error code using Express.js.

In this article, we’ll look at how to specify HTTP error code using Express.js.

How to specify HTTP error code using Express.js?

To specify HTTP error code using Express.js, we can use the res.status method.

For instance, we write

res.status(400);
res.send('error');

to call res.status with 400 to return a 400 response.

We also call res.send with the error message string to return an error message in the response body.

Conclusion

To specify HTTP error code using Express.js, we can use the res.status method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *