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();