简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM