當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js dnsPromises.resolveAny()用法及代碼示例


dnsPromises.resolveAny()方法是dns模塊的promises對象的內置應用程序編程接口,用於使用DNS協議解析指定主機名的所有記錄(即“ ANY”或“ *”)。

用法:

dnsPromises.resolveAny( hostname )

參數:該方法接受如上所述和以下描述的單個參數:


  • hostname:此參數指定一個字符串,該字符串表示要解析的主機名。

返回:此方法返回錯誤,記錄。
可能返回的記錄在其中

  • A: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



相關用法


注:本文由純淨天空篩選整理自anwesha0107大神的英文原創作品 Node.js | dnsPromises.resolveAny() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。