0

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?

mkr
  • 115
  • 1
  • 4
  • 12
  • Try setting in `mysql.conf`, comment `skip-networking`. The error is just a bad configuration of the MySQL server. Else you could try https://stackoverflow.com/a/19793797/7707749 this answer if it still doesn't work. – King Reload Jun 06 '17 at 07:59
  • In my case socket is just hang ups after some time. – G07cha Jun 06 '17 at 08:00
  • People who come here might also want to have a look at my comment here: https://stackoverflow.com/questions/64828363/rest-client-proxy-issue-in-vscode – aderchox Dec 12 '21 at 13:15

1 Answers1

0

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();
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31