Sometimes, we want to get error status code with Node.js http get with JavaScript.
In this article, we’ll look at how to get error status code with Node.js http get with JavaScript.
How to get error status code with Node.js http get with JavaScript?
To get error status code with Node.js http get with JavaScript, we use the statusCode
property.
For instance, we write
const https = require("https");
https
.get("https://example.com/", (res) => {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on("data", (d) => {
process.stdout.write(d);
});
})
.on("error", (e) => {
console.error(e);
});
to to make a request to example.com.
Then we get the status code of the request with res.statusCode
.
Conclusion
To get error status code with Node.js http get with JavaScript, we use the statusCode
property.