I'm very new to Node.js and I'm having an issue using node.dns.resolveNs function.
Some domains are completely down and it takes about a minute to get the response, which is usually "queryNs ETIMEOUT". Is there a way for me to set it to a shorter period, for example 10 seconds?
I am not sure of any way to set a timeout directly on the function call, but you could create a small wrapper around the call to handle timing out yourself:
var dns = require('dns');
var nsLookup = function(domain, timeout, callback) {
var callbackCalled = false;
var doCallback = function(err, domains) {
if (callbackCalled) return;
callbackCalled = true;
callback(err, domains);
};
setTimeout(function() {
doCallback(new Error("Timeout exceeded"), null);
}, timeout);
dns.resolveNs(domain, doCallback);
};
nsLookup('stackoverflow.com', 1000, function(err, addresses) {
console.log("Results for stackoverflow.com, timeout 1000:");
if (err) {
console.log("Err: " + err);
return;
}
console.log(addresses);
});
nsLookup('stackoverflow.com', 1, function(err, addresses) {
console.log("Results for stackoverflow.com, timeout 1:");
if (err) {
console.log("Err: " + err);
return;
}
console.log(addresses);
});
The output for the above script:
Results for stackoverflow.com, timeout 1:
Err: Error: Timeout exceeded
Results for stackoverflow.com, timeout 1000:
[ 'ns1.serverfault.com',
'ns2.serverfault.com',
'ns3.serverfault.com' ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With