📜  Node.js dnsPromises.resolveAny() 方法

📅  最后修改于: 2022-05-13 01:56:26.692000             🧑  作者: Mango

Node.js dnsPromises.resolveAny() 方法

dnsPromises.resolveAny() 方法是 dns 模块的 Promise 对象的内置应用程序编程接口,用于使用 DNS 协议解析指定主机名的所有记录(即“ANY”或“*”)。

句法:

dnsPromises.resolveAny( hostname )

参数:此方法接受如上所述和如下所述的单个参数:

  • 主机名:此参数指定一个字符串,表示要解析的主机名。

返回:此方法返回错误,记录。
可能返回的记录包括

  • 答: IPv4地址
  • AAAA: IPv6 地址
  • ANY:任何记录
  • CNAME:规范名称记录
  • MX:邮件交换记录
  • NAPTR:名称权限指针记录
  • NS:名称服务器记录
  • PTR:指针记录
  • SOA:权限记录的开始
  • SRV:服务记录
  • TXT:文本记录

下面的示例说明了 Node.js 中 dnsPromises.resolveAny() 方法的使用:

示例 1:

// Node.js program to demonstrate the   
// dnsPromises.resolveAny() method
  
// Accessing promises object from dns module
const dns = require('dns');
const dnsPromises = dns.promises;
   
// Calling dnsPromises.resolveAny() method 
dnsPromises.resolveAny('geeksforgeeks.org').then((res) => {
    console.log("for geeksforgeeks : ");
    console.log(res);
});
   
// Calling dnsPromises.resolveAny() method 
dnsPromises.resolveAny('localhost').then((res) => {
    console.log("for localhost : ");
    console.log(res);
});

输出:

for localhost :
[ { address: '127.0.0.1', ttl: 0, type: 'A' } ]
for geeksforgeeks :
[ { exchange: 'alt1.aspmx.l.google.com', priority: 5, type: 'MX' },
  { exchange: 'alt2.aspmx.l.google.com', priority: 5, type: 'MX' },
  { exchange: 'aspmx.l.google.com', priority: 1, type: 'MX' },
  { exchange: 'alt3.aspmx.l.google.com', priority: 10, type: 'MX' },
  { exchange: 'alt4.aspmx.l.google.com', priority: 10, type: 'MX' },
  { value: 'ns-1520.awsdns-62.org', type: 'NS' },
  { value: 'ns-1569.awsdns-04.co.uk', type: 'NS' },
  { value: 'ns-245.awsdns-30.com', type: 'NS' },
  { value: 'ns-869.awsdns-44.net', type: 'NS' },
  { entries:
     [ 'v=spf1 include:amazonses.com include:_spf.google.com -all' ],
    type: 'TXT' },
  { entries: [ 'fob1m1abcdp777bf2ncvnjm08n' ], type: 'TXT' },
  { nsname: 'ns-869.awsdns-44.net',
    hostmaster: 'awsdns-hostmaster.amazon.com',
    serial: 1,
    refresh: 7200,
    retry: 900,
    expire: 1209600,
    minttl: 86400,
    type: 'SOA' } ]

示例 2:

// Node.js program to demonstrate the   
// dnsPromises.resolveAny() method
  
// Accessing promises object from dns module
const dns = require('dns');
const dnsPromises = dns.promises;
   
// Calling dnsPromises.resolveAny() method 
// asynchronously 
(async function() {
      
    // Records from resolveAny function
    const records = await dnsPromises.resolveAny('geeksforgeeks.org');
      
    // Printing  records
    console.log("from async: ");
    console.log(records);   
})();

输出:

from async:
[ { exchange: 'alt4.aspmx.l.google.com', priority: 10, type: 'MX' },
  { exchange: 'alt1.aspmx.l.google.com', priority: 5, type: 'MX' },
  { exchange: 'alt2.aspmx.l.google.com', priority: 5, type: 'MX' },
  { exchange: 'aspmx.l.google.com', priority: 1, type: 'MX' },
  { exchange: 'alt3.aspmx.l.google.com', priority: 10, type: 'MX' },
  { value: 'ns-869.awsdns-44.net', type: 'NS' },
  { value: 'ns-1520.awsdns-62.org', type: 'NS' },
  { value: 'ns-245.awsdns-30.com', type: 'NS' },
  { value: 'ns-1569.awsdns-04.co.uk', type: 'NS' },
  { entries:
     [ 'v=spf1 include:amazonses.com include:_spf.google.com -all' ],
    type: 'TXT' },
  { entries: [ 'fob1m1abcdp777bf2ncvnjm08n' ], type: 'TXT' },
  { nsname: 'ns-869.awsdns-44.net',
    hostmaster: 'awsdns-hostmaster.amazon.com',
    serial: 1,
    refresh: 7200,
    retry: 900,
    expire: 1209600,
    minttl: 86400,
    type: 'SOA' } ]

注意:以上程序将使用node index.js命令编译运行。

参考: https://nodejs.org/api/dns.html#dns_dnspromises_resolveany_hostname