错误:连接ECONNREFUSED node.js https.get请求

时间:2016-08-31 11:24:51

标签: javascript node.js https get

我在一个小型测试节点项目中运行以下内容。

var https = require('https');

var request = https.get('https://example.com/script.json', function(response){
    console.dir(response);
});

request.on('error', function(){
    console.log(err);
});

当我尝试console.dir响应时,我收到以下错误。

throw er; // Unhandles 'error' event
Error: connect ECONNREFUSED xxx.xx.xxx.xx:xxx

它是对外部服务器上的json文件的简单获取请求。我知道该文件存在,所以我不确定为什么我会收到上述错误。该文件可以在浏览器中运行,无需身份验证。

更新:我编辑了代码。创建了一个承诺并添加了.on('error'...来解决任何问题。

现在输出以下内容:

( [Error: connect ECONNREFUSED xxx.xx.xxx.xx:xxx]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: 'xxx.xxx.xxx.xxx',
  port: 443 )

2 个答案:

答案 0 :(得分:1)

感谢@robertklep的帮助,我提出了以下解决方案,允许我使用请求从代理服务器后面连接到HTTPS

const https = require('https');
const request = require('request');

request(
    {
    'url':'https://https://example.com/script.json',
    'proxy':'http://xx.xxx.xxx.xx'
    },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
    }
);

答案 1 :(得分:0)

其他服务器未启动,或者没有收听443端口,因此您拒绝连接。 另一个选择是nodejs app将该示例url解析为与你的pc不同的ip。

相关问题