dns.resolve(hostname[, rrtype], callback)


  • hostname <string> 要解析的主机名。
  • rrtype <string> 资源记录类型。默认值: 'A'
  • callback <Function>
    • err <Error>
    • records 字符串数组 | 对象数组 | 对象

使用 DNS 协议将主机名(例如 'nodejs.org')解析为资源记录数组。callback 函数的参数为 (err, records)。成功时,records 将是一个资源记录数组。单个结果的类型和结构取决于 rrtype

🌐 Uses the DNS protocol to resolve a host name (e.g. 'nodejs.org') into an array of the resource records. The callback function has arguments (err, records). When successful, records will be an array of resource records. The type and structure of individual results varies based on rrtype:

rrtyperecords 包含结果类型简写方法
'A'IPv4 地址(默认)<string>dns.resolve4()
'AAAA'IPv6 地址<string>dns.resolve6()
'ANY'任意记录<Object>dns.resolveAny()
'CAA'CA 授权记录<Object>dns.resolveCaa()
'CNAME'规范名称记录<string>dns.resolveCname()
'MX'邮件交换记录<Object>dns.resolveMx()
'NAPTR'名称权威指针记录<Object>dns.resolveNaptr()
'NS'名称服务器记录<string>dns.resolveNs()
'PTR'指针记录<string>dns.resolvePtr()
'SOA'权威记录起始<Object>dns.resolveSoa()
'SRV'服务记录<Object>dns.resolveSrv()
'TXT'文本记录<string[]>dns.resolveTxt()

出现错误时,err 是一个 Error 对象,其中 err.codeDNS 错误代码 之一。

🌐 On error, err is an Error object, where err.code is one of the DNS error codes.