Categories
JavaScript Answers

How to get data out of a Node.js http get request?

Spread the love

To get data out of a Node.js http get request, we listen for the data event.

For instance, we write

require("http").get("http://httpbin.org/ip", (res) => {
  res.setEncoding("utf8");
  res.on("data", (body) => {
    console.log(body);
  });
});

to call res.on to listebn to the data event.

We get the response body from the body parameter in the callback.

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 *