Faizan Ahmed
Faizan Ahmed

Reputation: 214

Node Fetch resolve host like curl request

can we replicate curl resolve host in node-fetch or any other node http library.

curl https://www.example.net --resolve www.example.net:443:127.0.0.1

Upvotes: 0

Views: 1451

Answers (1)

harttle
harttle

Reputation: 98

You don't need another lib to do this. The builtin http module works fine:

const http = require('http');

const options = {
  hostname: '2606:2800:220:1:248:1893:25c8:1946',
  port: 80,
  path: '/',
  method: 'GET',
  headers: {
    'Host': 'example.com'
  }
};

const req = http.request(options, (res) => {
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.');
  });
});

req.end();

In HTTP protocol, the host name is in headers, and the IP used to establish the TCP connection is independent from that.

Upvotes: 1

Related Questions