node.js - 如何增加 Node.js 中 dns.resolve4() 的超时时间?

标签 node.js dns

如何增加 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/

相关文章:

linux - Docker 容器可以访问 DNS 但不能解析主机

http - 无法访问我的网站 ERR_TOO_MANY_REDIRECTS

c# - 如何确定 c# 中的 dns 更改?

c - 在端口 53 上的 C 中获取 DNS 查询。要绑定(bind)到的地址是什么?

javascript - Node.js 谷歌云平台数据存储日期比较

node.js - 从数据库获取数据的问题, "render"函数错误

mysql - 使用 node.js 在 MySQL 中创建表时出错

javascript - 无法在此 Node 中添加超时 http get 请求

javascript - Firebase 托管 + 云功能

c - c中的DNS客户端程序