How to specify HTTP error code using Express.js?

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.