• At the moment I'm afraid there isn't anything built-in. I should probably implement the error event on the HTTP request though, which would make this kind of thing a lot easier.

    Amazingly it doesn't seem to be listed as an event in the node.js docs despite being there, which is probably one reason I never implemented it. I'll add it to the bug list :)

    For now, you could actually look inside the CientRequest structure to see if it's managed to get a socket - the test will work even for 'unable to locate host' errors too. I think you should be able to do:

    var http = require("http");
    var req = http.get("http://www.espruino.com", function(res) {
      res.on('data', function(data) {
        console.log(data);
      });
    });
    if (!(req.sckt>0)) console.log("oops.");
    

    Other option is to make your own http get function that'd look a bit like:

    function get(url, callback) {
      var timeout = setTimeout(function() {
        timeout = undefined;
        console.log("Retrying");
        get(url, callback);
      }, 10000);
      http.get(url, function(res) {
       if (timeout===undefined) return; // too late, already retried
       clearTimeout(timeout);
       callback(res);
      });
    }
    
About

Avatar for Gordon @Gordon started