【问题标题】:Nodejs - https.request Error: connect ECONNREFUSEDNodejs - https.request 错误:连接 ECONNREFUSED
【发布时间】:2017-06-06 07:53:17
【问题描述】:

我正在尝试在我的节点服务器中进行 http 调用(我正在尝试的 URL 是 skackexchange 公共 url),下面是代码

httpsServer.js

var https = require('https');

function httpsRequest() {
    var options = {
        hostname: 'api.stackexchange.com',
        path: '/2.2/answers',
        method: 'GET'
    };

    var req = https.request(options, (res) => {
        res.on('data', function (data) {
            console.dir(data);
        });
        req.end();
    });

    req.on('error', function (e) {
        console.error(e);
    });
}

httpsRequest();

我在我的控制台中做了node httpsServer

但我收到以下错误。

Error:

{ Error: connect ECONNREFUSED xxx.xxx.xxx.xx:443
    at Object.exports._errnoException (util.js:1050:11)
    at exports._exceptionWithHostPort (util.js:1073:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1093:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: 'xxx.xxx.xxx.xx',
  port: 443 }

注意:我在代理服务器后面。

我做错了什么?如果是因为我在代理后面,有没有办法让它工作?

【问题讨论】:

标签: node.js econnrefused


【解决方案1】:

您将req.end() 放在错误的位置。您需要先结束请求,然后才能从服务器获得响应。

var https = require('https');
function httpsRequest() {
    var options = {
        hostname : 'api.stackexchange.com',
        path : '/2.2/answers',
        method : 'GET'
    };
    var req = https.request(options, (res) => {
        res.on('data', function (data) {
            console.dir(data);
        });
    });
    req.on('error', function (e) {
        console.error(e);
    });
    req.end(); // correct place
}
httpsRequest();

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 2015-12-23
    • 2022-11-03
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多