Categories
JavaScript Answers

How to get error status code with Node.js http get with JavaScript?

Spread the love

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.

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 *