node.js - Check if URL would result in getaddrinfo error -
is there way check if given address result in getaddrinfo error when request made? have list of urls , of them causes getaddrinfo error , crashs node.js app. check if address can resolved before call request method. have been trying catch error inside callback function of request without luck. thanks!
this 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 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 error ----" + err); }); req.end();
edit : error node.js -
error: getaddrinfo enotfound @ errnoexception (dns.js:37:11) @ object.onanswer [as oncomplete] (dns.js:124:16)
you can catch error
event on http request. example:
var dogetrequest = require("http").get; var req = dogetrequest("http://foo.bar", function(res) { }); req.on("error", function errorhandler(err) { if(err.code === "enotfound") { console.log("address not found.", e); } else { console.log("some other error:", e); } });
the reason why event used , not exception because hostname lookup asynchronous. not sure why don't pass first parameter callback, that's way things are.
Comments
Post a Comment