Node.js dns.resolveMx() 方法
dns.resolveMx() 方法是 dns 模块的内置应用程序编程接口,用于使用 DNS 协议解析指定主机名的 MX 或邮件交换记录。
句法:
dns.resolveMx( hostname, callback )
参数:此方法有两个参数,如前所述,如下所述:
- 主机名:此参数指定一个字符串,表示要解析的主机名。
- callback:指定DNS解析主机名后调用的函数。
- 错误:如果生成则指定错误。
- 地址:它是一个字符串数组,表示返回的主机名邮件交换记录。
返回值:该方法通过回调函数返回错误,地址。这些数据作为参数传递给回调函数。
下面的例子说明了 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