Get an HTML Page in Node.js

Contents
In this article, you will learn how to get an HTML page in Node.js.
Getting an HTML page in Node.js
To get an HTML page in Node.js, you can use the built-in http or https module to send an HTTP GET request to the server and receive the HTML page as a response. Here are the steps to do so:
Import the required modules
const https = require('https');
Define the options for the request
Define the options for the request, including the server URL and any other required options.
const options = {
hostname: 'example.com',
port: 443,
path: '/page',
method: 'GET'
};
Send the request using the https.request() method
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(data);
});
});
req.on('error', (error) => {
console.error(error);
});
req.end();
Here’s the complete example:
const https = require('https');
const options = {
hostname: 'example.com',
port: 443,
path: '/page',
method: 'GET'
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(data);
});
});
req.on('error', (error) => {
console.error(error);
});
req.end();
This example sends a GET request to https://example.com/page and logs the HTML page content to the console. If the server returns a compressed response (e.g. gzip), you can use the zlib module to decompress the response.
const https = require('https');
const zlib = require('zlib');
const options = {
hostname: 'example.com',
port: 443,
path: '/page',
method: 'GET',
headers: {
'Accept-Encoding': 'gzip'
}
};
const req = https.request(options, (res) => {
let data = '';
const encoding = res.headers['content-encoding'];
const decompress = encoding === 'gzip' ? zlib.createGunzip() : zlib.createInflate();
res.pipe(decompress);
decompress.on('data', (chunk) => {
data += chunk;
});
decompress.on('end', () => {
console.log(data);
});
});
req.on('error', (error) => {
console.error(error);
});
req.end();
This example sets the Accept-Encoding header to gzip to request a compressed response. It uses the zlib module to decompress the response if it’s compressed.