Getting connect ECONNREFUSED 127.0.0.1:80 when attempting HTTP request
Solution 1
In my case, issue was actually default behaviour of HTTP client that I was using, axios
.
By default, axios redirects us to 127.0.0.1:80
if it doesn't find requested URL or http
method(GET/POST/PUT). So better check your URL if are also using axios
.
Solution 2
My problem was while using supertest
and 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.
Solution 3
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)
Solution 4
There are several issues here:
The
hostname
field of theoptions
structure 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
request
method 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 usehttps
everywhere instead ofhttp
.
ng-hacker-319
Updated on January 19, 2022Comments
-
ng-hacker-319 over 2 years
I am attempting to make a http request to news.google.com using the native node.js
http
module. I am getting theconnect ECONNREFUSED 127.0.0.1:80
error when I tried the followingvar 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
request
npm module. And it worked!