如何增加 Node.js 中 dns 解析的超时时间?我正在尝试解析网址以查看可用的内容,但很多请求超时,并且可能出现误报。
// checks a url string for availability and errors on 'err'
function checkAvailable( url ) {
dns.resolve4( url, function (err, addresses) {
if (err) console.log (url + " : " + err)
})
}
最佳答案
Node.js DNS 模块是 c-ares 的包装器并且选项非常少。如果您需要提供任何(例如超时),我建议您查看 node-dns ,它为 DNS 模块中的所有可用功能提供 1:1 映射,以及用于指定更高级选项(包括超时)的附加方法:
var dns = require('native-dns');
var question = dns.Question({
name: 'www.google.com',
type: 'A'
});
var req = dns.Request({
question: question,
server: { address: '8.8.8.8', port: 53, type: 'udp' },
timeout: 1000
});
req.on('timeout', function () {
console.log('Timeout in making request');
});
req.on('message', function (err, answer) {
answer.answer.forEach(function (a) {
console.log(a.promote().address);
});
});
req.on('end', function () {
console.log('Finished processing request');
});
req.send();
关于node.js - 如何增加 Node.js 中 dns.resolve4() 的超时时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11388318/