node.js 尝试 HTTP 请求时获取连接 ECONNREFUSED 127.0.0.1:80
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39910746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting connect ECONNREFUSED 127.0.0.1:80 when attempting HTTP request
提问by ng-hacker-319
I am attempting to make a http request to news.google.com using the native node.js
httpmodule. I am getting theconnect ECONNREFUSED 127.0.0.1:80error when I tried the following
我正在尝试使用本机 node.js
http模块向 news.google.com 发出 http 请求。我得到的connect ECONNREFUSED 127.0.0.1:80错误,当我尝试以下
var http = require('http');
var payload = JSON.stringify({
name: 'John Smith',
email: '[email protected]',
resume: 'https://drive.google.com/open?id=asgsaegsehsehseh'
});
var options = {
hostname: 'https://news.google.com',
path: '/',
method: 'GET'
};
var httpRequest = http.request(options, function(request, response) {
console.log('STATUS', response.statusCode);
response.setEncoding('utf8');
response.on('data', function(chunk) {
console.log('BODY:', chunk);
});
response.on('end', function() {
console.log('No more data in response');
});
});
httpRequest.on('error', function(e) {
console.log('Error with the request:', e.message);
});
httpRequest.write(payload);
httpRequest.end();
Why am I getting this error?
为什么我收到这个错误?
I tried using the requestnpm module. And it worked!
我尝试使用requestnpm 模块。它奏效了!
回答by Jaspreet Singh
In my case, issue was actually default behaviour of HTTP client that I was using, axios.
就我而言,问题实际上是我使用的 HTTP 客户端的默认行为,axios.
By default, axios redirects us to 127.0.0.1:80if it doesn't find requested URL or httpmethod(GET/POST/PUT). So better check your URL if are also using axios.
默认情况下,127.0.0.1:80如果axios没有找到请求的 URL 或http方法(GET/POST/PUT),它会将我们重定向到。所以最好检查你的 URL 如果也使用axios.
回答by arispen
My problem was while using supertestand jest. My mistake was not putting "/" as a prefix to some url. So, double check if the url for the request you are making is proper.
我的问题是在使用supertestand 时jest。我的错误是没有将“/”作为某些 url 的前缀。因此,请仔细检查您提出的请求的 url 是否正确。
回答by keithmo
There are several issues here:
这里有几个问题:
The
hostnamefield of theoptionsstructure should be just the host, not a URL. In your case it should just be'news.google.com'.The signature for the callback to the
requestmethod isfunction (response)-- yours isfunction (request, response). Lose the first parameter.As written this will aways return an HTTP redirection to the https site. Replace
var http = require('http');withvar https = require('https');and then usehttpseverywhere instead ofhttp.
结构的
hostname字段options应该只是主机,而不是 URL。在您的情况下,它应该只是'news.google.com'.request方法回调的签名是function (response)——你的function (request, response)。丢失第一个参数。正如所写的那样,这将返回到 https 站点的 HTTP 重定向。替换
var http = require('http');为var https = require('https');,然后https在任何地方使用,而不是http。
回答by Frostack
I'm using axios and this error occurred with get request, solved it by adding http:// before my url (in my case the server is http)
我正在使用 axios 并且这个错误发生在 get 请求中,通过在我的 url 之前添加 http:// 解决了它(在我的情况下服务器是 http)

