我无法在节点中发出Web请求
我正在做一个简单的Http请求到谷歌的主页,而不是在控制台中logging数据显示此错误
错误:连接ETIMEDOUT 172.217.26.163:80
此外,我试图向谷歌的地理位置API请求,也没有得到我的任何数据。
这是我的代码(Google的主页版本)
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完成了。
您应该在传递给http.request
的options
传递指向代理的host
和port
variables。
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); });