https.request Error: connect ECONNREFUSED

I'm trying to make a http call in my node server(URL I'm trying is the skackexchange public url), below is the code

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

And and i did node httpsServer in my console

But I am getting the below error.

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 }

NOTE: I'm behind a proxy server.

What am i doing wrong? If its because I'm behind proxy is there a way to make it work?


You have req.end() in wrong place. You need to end your request before you can get response from server.

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();
链接地址: http://www.djcxy.com/p/87254.html

上一篇: 在Atlassian Bamboo中更改执行构建脚本的用户

下一篇: https.request错误:连接ECONNREFUSED