Node.js DNS模块

DNS

  • dns.getServers()
  • dns.lookup(hostname[, options], callback)
    • Supported getaddrinfo flags
  • dns.lookupService(address, port, callback)
  • dns.resolve(hostname[, rrtype], callback)
  • dns.resolve4(hostname, callback)
  • dns.resolve6(hostname, callback)
  • dns.resolveCname(hostname, callback)
  • dns.resolveMx(hostname, callback)
  • dns.resolveNaptr(hostname, callback)
  • dns.resolveNs(hostname, callback)
  • dns.resolveSoa(hostname, callback)
  • dns.resolveSrv(hostname, callback)
  • dns.resolvePtr(hostname, callback)
  • dns.resolveTxt(hostname, callback)
  • dns.reverse(ip, callback)
  • dns.setServers(servers)
  • Error codes
  • Implementation considerations
    • dns.lookup()
    • dns.resolve(), dns.resolve*() and dns.reverse()

The dns module contains functions belonging to two different categories:

DNS模块包含属于两个不同类别的函数
在node命令行模式被默认引入,但在node脚本中需要显示引入

const dns = require('dns'); 

1) Functions that use the underlying operating system facilities to perform name resolution, and that do not necessarily perform any network communication. This category contains only one function: dns.lookup(). Developers looking to perform name resolution in the same way that other applications on the same operating system behave should use dns.lookup().

2) Functions that connect to an actual DNS server to perform name resolution, and that always use the network to perform DNS queries. This category contains all functions in the dns module except dns.lookup(). These functions do not use the same set of configuration files used by dns.lookup() (e.g. /etc/hosts). These functions should be used by developers who do not want to use the underlying operating system’s facilities for name resolution, and instead want to always perform DNS queries.

1) 该函数使用操作系统底层的工具来执行域名解析,并且任何网络通信都是非必要的。该目录仅包含一个函数:dns.lookup()。如果开发者执行域名解析的系统和对方应用程序的操作系统一样,那么请使用dns.lookup()。

2) 该函数连接到一个实际的域名服务器来执行域名解析工作,并且总是通过网络来执行域名解析查询。该目录包含dns模块除了dns.lookup()之外的所有函数。这些函数不使用dns.lookup()的配置文件(例如 /etc/hosts)。这些函数推荐被那些不想使用底层操作系统工具进行域名解析的开发人员调用,来代替常用的DNS查询。

There are subtle consequences in choosing one over the other, please consult the Implementation considerations section for more information.

在一个接一个地选择的时候结果是非常微妙的,请咨询实施注意事项部分获得更多信息。

dns.lookup(hostname[, options], callback)

将域名(比如 ‘runoob.com’)解析为第一条找到的记录 A (IPV4)或 AAAA(IPV6)。参数 options可以是一个对象或整数。如果没有提供 options,IP v4 和 v6 地址都可以。如果 options 是整数,则必须是 4 或 6。

dns.lookup('nodejs.org', (err, address, family) =>{
    if(err) {
        return console.error(err);
    }
    console.log('lookup address: ' + address + ', family: ' + family);
});
//输出 lookup address: 104.20.22.46, family: 4

dns.resolve4(hostname, callback)

和 dns.resolve() 类似, 仅能查询 IPv4 (A 记录)。 addresses IPv4 地址数组 (比如,[‘74.125.79.104’, ‘74.125.79.105’, ‘74.125.79.106’])。

dns.resolve4('nodejs.org', (err, address) => {
    if(err) {
        throw err;
    }
    console.log('resolve4 address.length: '+ address.length);
    address.forEach((a) => {
        console.log(`resolve4 address: ${a}`);
    });
});
/*
输出
resolve4 address.length: 2
resolve4 address: 104.20.23.46
resolve4 address: 104.20.22.46
*/

dns.reverse(ip, callback)

反向解析 IP 地址,指向该 IP 地址的域名数组。

dns.lookup('www.csdn.net', (err, address, family) => {
    if(err) {
        return console.error(err);
    }
    dns.reverse(address, (err, hostnames) => {
        if(err) {
            return console.log(err);
        }
        console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);
    });
});

rrtypes

var types = ['A','AAAA','MX','TXT','SRV','PTR','NS','CNAME'];  

以下列出了 dns.resolve() 方法中有效的 rrtypes值:

  • ‘A’ IPV4 地址, 默认
  • ‘AAAA’ IPV6 地址
  • ‘MX’ 邮件交换记录
  • ‘TXT’ text 记录
  • ‘SRV’ SRV 记录
  • ‘PTR’ 用来反向 IP 查找
  • ‘NS’ 域名服务器记录
  • ‘CNAME’ 别名记录
  • ‘SOA’ 授权记录的初始值

错误码

每次 DNS 查询都可能返回以下错误码:

  • dns.NODATA: 无数据响应。
  • dns.FORMERR: 查询格式错误。
  • dns.SERVFAIL: 常规失败。
  • dns.NOTFOUND: 没有找到域名。
  • dns.NOTIMP: 未实现请求的操作。
  • dns.REFUSED: 拒绝查询。
  • dns.BADQUERY: 查询格式错误。
  • dns.BADNAME: 域名格式错误。
  • dns.BADFAMILY: 地址协议不支持。
  • dns.BADRESP: 回复格式错误。
  • dns.CONNREFUSED: 无法连接到 DNS 服务器。
  • dns.TIMEOUT: 连接 DNS 服务器超时。
  • dns.EOF: 文件末端。
  • dns.FILE: 读文件错误。
  • dns.NOMEM: 内存溢出。
  • dns.DESTRUCTION: 通道被摧毁。
  • dns.BADSTR: 字符串格式错误。
  • dns.BADFLAGS: 非法标识符。
  • dns.NONAME: 所给主机不是数字。
  • dns.BADHINTS: 非法HINTS标识符。
  • dns.NOTINITIALIZED: c c-ares 库尚未初始化。
  • dns.LOADIPHLPAPI: 加载 iphlpapi.dll 出错。
  • dns.ADDRGETNETWORKPARAMS: 无法找到
  • GetNetworkParams 函数。
  • dns.CANCELLED: 取消 DNS 查询。

其它方法感觉不常用,暂时跳过不介绍了

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值