To get a JSON via HTTP request in Node, we call request
with an object with the json
property set to true
.
For instance, we write
const options = {
hostname: "127.0.0.1",
port: app.get("port"),
path: "/users",
method: "GET",
json: true,
};
request(options, (error, response, body) => {
if (error) {
console.log(error);
} else {
console.log(body);
}
});
to call request
with an object with the json
property set to true
.
We make a get request to the URL we get from the hostname
, port
, and path
.
Since we get json
to true
, the response we get from the body
parameter is a plain JavaScript object.