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
var http = require('http');
var payload = JSON.stringify({ name: 'John Smith', email: '', resume: '
});
var options = { hostname: ' 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!
9 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.
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.
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)
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.
I encountered this issue while using supertest and jest. I made the mistake of using ./users instead of /users as the url.
I had the same problem. Solve it by fixing the HTTP request path mistake in my code.
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.
1store 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 = '"
axios.get(baseUrl + '/api/myurl')
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 =