dns.resolveMx()方法是dns模塊的內置應用程序編程接口,用於使用DNS協議解析指定主機名的MX或郵件交換記錄。
用法:
dns.resolveMx( hostname, callback )
參數:此方法具有上述和以下所述的兩個參數:
- hostname:此參數指定一個字符串,該字符串表示要解析的主機名。
- callback:它指定在主機名進行DNS解析之後要調用的函數。
- error:如果生成,則指定錯誤。
- addresses:它是一個字符串數組,表示返回的主機名郵件交換記錄。
返回值:該方法通過回調函數返回錯誤地址。這些數據作為參數傳遞給回調函數。
以下示例說明了Node.js中dns.resolveMx()方法的使用:
範例1:
// Node.js program to demonstrate the
// dns.resolveMx() method
// Accessing dns module
const dns = require('dns');
// Calling dns.resolveMx() method for hostname
// geeksforgeeks.org and displaying them in
// console as a callback
dns.resolveMx('geeksforgeeks.org', (err,
addresses) => console.log('mx records:%j', addresses));
輸出:
mx records:[ {"exchange":"aspmx.l.google.com","priority":1}, {"exchange":"alt3.aspmx.l.google.com","priority":10}, {"exchange":"alt4.aspmx.l.google.com","priority":10}, {"exchange":"alt1.aspmx.l.google.com","priority":5}, {"exchange":"alt2.aspmx.l.google.com","priority":5} ]
範例2:
// Node.js program to demonstrate the
// dns.resolveMx() method
// Accessing dns module
const dns = require('dns');
// Calling dns.resolveMx() method for
// hostname google.com and displaying
// them in console as a callback
dns.resolveMx('google.com', (err,
addresses) => console.log('mx records:%j', addresses));
輸出:
mx records:[ {"exchange":"alt3.aspmx.l.google.com","priority":40}, {"exchange":"aspmx.l.google.com","priority":10}, {"exchange":"alt2.aspmx.l.google.com","priority":30}, {"exchange":"alt1.aspmx.l.google.com","priority":20}, {"exchange":"alt4.aspmx.l.google.com","priority":50} ]
注意:上麵的程序將通過使用node index.js
命令。
參考: https://nodejs.org/api/dns.html#dns_dns_resolvemx_hostname_callback
相關用法
注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 Node.js | dns.resolveMx() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。