【问题标题】:I'm not able to make Web requests in node我无法在节点中发出 Web 请求
【发布时间】:2018-01-29 13:05:43
【问题描述】:

我正在向 google 的主页发出一个简单的 Http 请求,但不是在控制台中记录数据,而是显示此错误

错误:连接 ETIMEDOUT 172.217.26.163:80

我还尝试向 google 的地理位置 API 发出请求,但也没有返回任何数据。

这是我的代码(谷歌主页版本)

const http = require('http');

let req = http.request('http://www.google.co.uk', function(response){
    console.log(response.statusCode);
    response.pipe(process.stdout);
});

req.end();

我也在代理服务器后面,可能是因为代理服务器吗?如果是,我如何在 node.js 本身中设置代理服务器。我已经为 npm 做到了。

【问题讨论】:

  • 这可能与您的代理服务器有关,因为您没有在请求设置中声明 - 请参阅 the docs

标签: node.js http


【解决方案1】:

您应该在传递给http.requestoptions 中传递指向代理的hostport 变量。

   var options = {
      host: "proxypath",
      port: 8080,
      path: "http://www.google.co.uk",
      headers: {
        Host: "http://www.google.co.uk"
      }
    };
    http.request(options, function(response) {
      console.log(response.statusCode);
      response.pipe(process.stdout);
    });

【讨论】:

  • 成功了,非常感谢。您能否详细说明一下幕后发生的事情?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多