Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Error: connect ECONNREFUSED when using http.request

Tags:

node.js

I am troubleshooting a Node.js script, and have stripped out almost all of the code and still am able to reproduce the following error:

{ 
  [Error: connect ECONNREFUSED]
  stack: 'Error: connect ECONNREFUSED 
          at exports._errnoException (util.js:682:11) 
          at Object.afterConnect [as oncomplete] (net.js:947:19)',
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' 
}

The entire script is:

var http = require('http');

http.get("http://api.hostip.info/get_json.php", function(res) {
    console.log("Received response: " + res.statusCode);
});

var req = http.request(function(res) {
    console.log("Request began");
    var output = '';

    res.on('data', function (chunk) {
        output += chunk;
    });

    res.on('end', function () {
        console.log('Request complete:');
        console.log(output);
    });
});

req.on('error', function (err) {
    console.log(err);
    //console.log('error: ' + err.message);
});

req.end();
console.log("Script complete");

I'm confident this is a simple mistake somewhere in the code, but have been unable to identify the problem?

like image 802
jfcallo Avatar asked Dec 10 '13 07:12

jfcallo


People also ask

What is error connect Econnrefused?

The ECONNREFUSED Connection refused by the server error, is a common error returned by the Filezilla FTP client. This error indicates that the user is trying to connect to your server and is unable to connect to the port. There are a few reasons the client would receive this error.

What is connect Etimedout?

ETIMEDOUT (Operation timed out): A connect or send request failed because the connected party did not properly respond after a period of time. Usually encountered by HTTP or net. Often a sign that a socket.


1 Answers

You haven't provided a url in http.request.

Try var req = http.request("someurlhere", function(res) { ... etc.

Moreover, if you're using http.request like that, I can't quite see the purpose of the following block of code at all (maybe remnants from the rest of the completed script?)

http.get("http://api.hostip.info/get_json.php", function(res) {
    console.log("Received response: " + res.statusCode);
});
like image 193
brandonscript Avatar answered Sep 22 '22 04:09

brandonscript