【问题标题】:Node Fetch resolve host like curl requestNode Fetch 解析主机,如 curl 请求
【发布时间】:2022-01-24 22:53:40
【问题描述】:

我们能否在 node-fetch 或任何其他节点 http 库中复制 curl 解析主机。

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

【问题讨论】:

    标签: node.js node-fetch node-http-proxy node-https


    【解决方案1】:

    您不需要另一个库来执行此操作。内置的 http 模块工作正常:

    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();
    

    在HTTP协议中,主机名在header中,用于建立TCP连接的IP与之无关。

    【讨论】:

    • 错误:在 TCPConnectWrap.afterConnect 连接 ETIMEDOUT [as oncomplete] (node:net:1161:16) 在 ClientRequest 实例上发出“错误”事件:在 Socket.socketErrorListener (node:_http_client:447: 9) 在 Socket.emit (node:events:390:28) 在emitErrorNT (node:internal/streams/destroy:157:8) 在emitErrorCloseNT (node:internal/streams/destroy:122:3) 在processTicksAndRejections (node: internal/process/task_queues:83:21) { errno: -4039, code: 'ETIMEDOUT', syscall: 'connect', address: '' port: 80 } 它给出了这个错误
    • 你的服务器是https,请require('https')连接443端口(默认),参考nodejs.org/api/https.html
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 2021-05-22
    • 2015-11-02
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多