45

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: 'john.smith@smith.com',
    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!

ng-hacker-319
  • 609
  • 2
  • 6
  • 8

11 Answers11

94

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.

Jaspreet Singh
  • 1,672
  • 1
  • 14
  • 21
  • Thanks for your help. inside server of reactjs (ssr.tsx) the Axios url was not correct. – AmerllicA May 14 '21 at 22:21
  • If you're using nextjs, and using the same function to fetch data on client side and server side using relative url, it might not work on the server side. You need to give an absolute path on the server side. On client side, Axios automatically gets window url. – Badmaash Mar 06 '23 at 16:44
69

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.

arispen
  • 2,832
  • 1
  • 15
  • 13
10

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)

colin
  • 284
  • 2
  • 12
Frostack
  • 661
  • 11
  • 13
6

There are several issues here:

  1. The hostname field of the options structure should be just the host, not a URL. In your case it should just be 'news.google.com'.

  2. The signature for the callback to the request method is function (response) -- yours is function (request, response). Lose the first parameter.

  3. As written this will aways return an HTTP redirection to the https site. Replace var http = require('http'); with var https = require('https'); and then use https everywhere instead of http.

keithmo
  • 4,893
  • 1
  • 21
  • 17
2

I encountered this issue while using supertest and jest. I made the mistake of using ./users instead of /users as the url.

2

I had the same problem with jest and supertest. Just changed api/blogs to /api/blogs and it worked !

Abdelhk
  • 55
  • 4
0

I had the same problem. Solve it by fixing the HTTP request path mistake in my code.

0

I had this problem, and my solution (That might not be the same for most people) was that the server was not listening yet, I had to call the axios function after.

Zac Waite
  • 13
  • 6
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 16 '21 at 08:50
0

store the root url in some variable or environtment and append it before request so instead of axios.get('/api/myurl') do something like let baseUrl = 'https://google.com"

axios.get(baseUrl + '/api/myurl')

Baraja Swargiary
  • 381
  • 2
  • 10
0

Another cause for this error could be that in your code, you might be referring to an environment variable as:

const localhostUrl = process.env.LOCALHOST_URL;

Whilst, not having this variable defined, (as expected), in your .env file as:

LOCALHOST_URL = http://localhost:8000/
Chukwunazaekpere
  • 906
  • 1
  • 6
  • 13
0

I also got same error when when using supertest and jest. fix was adding prefix "/" to the url.