fidev
fidev

Reputation: 1252

Error: connect ECONNREFUSED node.js https.get request

I'm running the following in a small test node project.

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

When I try to console.dir the response I get the following error.

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

It's a simple get request to a json file on an external server. I know the file exists so I'm not sure why I'm getting the above error. The file can be run in the browser and requires no authentication.

UPDATE: I edited the code. created a promise and added an .on('error'... to catch any issues.

The following is now output:

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

Upvotes: 1

Views: 2077

Answers (2)

fidev
fidev

Reputation: 1252

Thanks to help from @robertklep I came up with the following solution which allows me to connect to HTTPS from behind a proxy using request

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

Upvotes: 1

sagie
sagie

Reputation: 1777

The other server is not up, or not listening to 443 port so you get a connection refused. Another option is that the nodejs app is resolving that example url differently to a different ip than your pc.

Upvotes: 0

Related Questions