Node.js错误:在使用http.request时连接ECONNREFUSED

我正在对Node.js脚本进行故障排除,并删除了几乎所有的代码,但仍然可以重现以下错误:

{ 
  [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' 
}

整个脚本是:

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");

我相信这是代码中的一个简单的错误,但一直无法识别问题?

您没有在http.request中提供网址。

尝试var req = http.request(“someurlhere”,function(res){…等

而且,如果你这样使用http.request,我完全看不到下面的代码块的目的(可能剩下的完整脚本的剩余部分)

http.get("http://api.hostip.info/get_json.php", function(res) {
    console.log("Received response: " + res.statusCode);
});
http://stackoverflow.com/questions/20488496/node-js-error-connect-econnrefused-when-using-http-request

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:Node.js错误:在使用http.request时连接ECONNREFUSED