const dns = require('dns');
const http = require('http');
// Step 1: Define the custom DNS server
const customDnsServer = '8.8.8.8';
// Step 2: Set the custom DNS server for the HTTP request
http.request({
hostname: 'example.com',
method: 'GET',
path: '/',
// Step 3: Configure the agent to use the custom DNS server
agent: new http.Agent({ lookup: (hostname, options, callback) => {
// Step 4: Use the custom DNS server for hostname resolution
dns.resolve4(hostname, { server: customDnsServer }, (err, addresses) => {
if (err) {
// Handle DNS resolution error
return callback(err);
}
// Provide the resolved IP addresses to the HTTP agent
callback(null, addresses, options);
});
}}),
}, (res) => {
// Step 5: Handle the HTTP response
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
// Process the complete response data
console.log(data);
});
}).end();