Node Js - Http Module

Https is the Core module provided by Node.js to make an Http Client for an API, This module gives you data in chunks(byte), that you have to concatenate and create complete data.
Whereas the third party module such as Request and postman-request packages returns you the complete data.


Creating the Http client using node.js core module



const https = require("https");
const url = "https://reqres.in/api/users?page=2";

const request = https.request(url, (response) => {
  let data = ''

  response.on("data", (datachunk) => {
	data = datachunk;
  });

  response.on("end", () => {
    console.log(data);
  });
});

request.on("error", (error) => {
  console.log("The error is:" + error);
});

request.end();


Output:



The above code returns the data chunks from the API, To create the JSON data from the data chunks, we need to append the data till the complete data ends.


Creating JSON output from the data Chunks 



The above code will print the JSON to the console.

The code for your reference:


const https = require("https");
const url = "https://reqres.in/api/users?page=2";

const request = https.request(url, (response) => {
 let data = ''
  response.on("data", (datachunk) => {

	data = data + datachunk.toString();
	
  });

  response.on("end", () => {

	console.log(JSON.parse(data));
	
  });
});

request.on("error", (error) => {
  console.log("The error is:" + error);
});

request.end();