0
votes

My application crashes after a while with this error:

{ Error: connect ETIMEDOUT 31.13.92.51:443 at Object.exports._errnoException (util.js:1018:11) at exports._exceptionWithHostPort (util.js:1041:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14) code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'connect',
address: '31.13.92.51', port: 443 }

{ Error: socket hang up at createHangUpError (_http_client.js:253:15) at TLSSocket.socketCloseListener (_http_client.js:285:23) at emitOne (events.js:101:20) at TLSSocket.emit (events.js:188:7) at _handle.close (net.js:497:12) at TCP.done [as _onclose] (_tls_wrap.js:332:7) code: 'ECONNRESET' }

I don't see anything wrong in my code, how can i handle this error?

try {
    https.get('https://www.instagram.com/'+username+'/?__a=1', function(resp){
        var body = '';
        resp.setEncoding('utf8');
        resp.on('data', function (chunk) {
            body += chunk;
        });
        resp.on('end', function () {
            try {
                var json = JSON.parse(body);
            } catch (error) {
                callback('Unable to fetch user info from Instagram.', null);
                return;
            }
            callback(null, json.user);
        });
    });
}catch (error){
    callback("Connection to Instagram failed.", null);
}
1

1 Answers

2
votes

I have tried the same with https module, I've had the same problem, so I decided to go with another module which is request module, it seemed to work fine. I don't know why but https can't get resp from instagram.com , as I've tried already. This is what you can try as it worked for me. Install request module

    var request = require("request");
    var ext = '/?__a=1';
    var username = //username here;


    request
      .get('https://www.instagram.com/'+ username + ext)
      .on('response', function(response) {
        console.log(response.statusCode) // 200 is what I get from request
        console.log(response.headers['content-type'])
      })