Is there a way to check if any given address would result in getaddrinfo error when request is made? I have list of URLs and some of them causes getaddrinfo error and crashs Node.js app. So I would like to check if an address can be resolved before I call request method. I have been trying to catch that error inside callback function of request without any luck. Thanks!
This is my attempt - ref = this;
var options = {
host: ref.host,
headers: {
'user-agent': 'Mozilla/5.0'
},
path: ref.pathname,
method: "GET",
timeout: 5000,
followRedirect: true,
maxRedirects: 5
};
// Process the file.
var file = fs.createWriteStream(path.normalize(ref.saveTo + ref.name + ref.extension));
var req = ref.protocol.get(options, function(res) {
res.on('data', function(data) {
f.write(data);
}).on('end', function() {
f.end();
});
});
req.on('response', function(err) {
if(err.statusCode == '404') {
console.log("Response ", err);
}
});
req.on('error', function(err) {
console.log("This is error ----" + err);
});
req.end();
Edit : This is the error I get from Node.js -
Error: getaddrinfo ENOTFOUND at errnoException (dns.js:37:11) at Object.onanswer [as oncomplete] (dns.js:124:16)