Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting connect ECONNREFUSED 127.0.0.1:80 when attempting HTTP request

I am attempting to make a http request to news.google.com using the native node.js http module. I am getting the connect ECONNREFUSED 127.0.0.1:80 error when I tried the following

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 request npm module. And it worked!

like image 338
ng-hacker-319 Avatar asked Oct 07 '16 06:10

ng-hacker-319


People also ask

What is connect Econnrefused?

ECONNREFUSED means no process is listening at the given address and port.

Could not send request error connect Econnrefused 127.0 0.1 8080?

econnrefused means most probably you have not started your server on port 8080. ECONNREFUSED (Connection refused): No connection could be made because the target machine actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host.


2 Answers

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.

like image 58
Jaspreet Singh Avatar answered Oct 18 '22 11:10

Jaspreet Singh


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.

like image 35
arispen Avatar answered Oct 18 '22 11:10

arispen