dnsPromises.resolve()方法是dns模块的promises对象的内置应用程序编程接口,用于将主机名解析为资源记录的数组。
用法:
dnsPromises.resolve( hostname, rrtype )
参数:此方法具有上述和以下所述的两个参数:
- hostname:此参数指定一个字符串,该字符串表示要解析的主机名。
- rrtype:它指定资源记录类型。其默认值为“ A”。值来自“ A”,“ AAAA”,“ ANY”,“ CNAME”,“ MX”,“ TXT”,“ NS”,“ NAPTR”,“ PTR”,“ SOA”,“ SRV”。
- A:IPv4地址
- AAAA:IPv6地址
- ANY:任何记录
- CNAME:规范名称记录
- MX:邮件交换记录
- NAPTR:名称权限指针记录
- NS:名称服务器记录
- PTR:指针记录
- SOA:授权记录的开始
- SRV:服务记录
- TXT:文字记录
返回值:此方法返回错误,记录。
以下示例说明了Node.js中dnsPromises.resolve()方法的使用:
范例1:
// Node.js program to demonstrate the
// dnsPromises.resolve() method
// Accessing promises object from dns module
const dns = require('dns');
const dnsPromises = dns.promises;
// Set the rrtype value for
// dnsPromises.resolve() method
const rrtype="NS";
// Calling dnsPromises.resolve() method
dnsPromises.resolve('geeksforgeeks.org', rrtype).then((res) => {
console.log(res);
});
输出:
[ 'ns-869.awsdns-44.net', 'ns-245.awsdns-30.com', 'ns-1520.awsdns-62.org', 'ns-1569.awsdns-04.co.uk' ]
范例2:
// Node.js program to demonstrate the
// dnsPromises.resolve() method
// Accessing promises object from dns module
const dns = require('dns');
const dnsPromises = dns.promises;
// Setting rrtype for dnsPromises.resolve() method
const rrtype = "MX";
// Calling dnsPromises.resolve() method asynchronously
(async function() {
// Records from resolve function
const records = await dnsPromises.resolve('geeksforgeeks.org', rrtype);
// Printing records
console.log("from async:");
console.log(records);
})();
输出:
from async: [ { exchange:'alt2.aspmx.l.google.com', priority:5 }, { 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 } ]
范例3:
// Node.js program to demonstrate the
// dnsPromises.resolve() method
// Accessing promises object from dns module
const dns = require('dns');
const dnsPromises = dns.promises;
// Set the rrtype for dnsPromises.resolve() method
const rrtype = "A";
// Calling dnsPromises.resolve() method
dnsPromises.resolve('geeksforgeeks.org', rrtype).then((res) => {
console.log(res);
});
输出:
[ '34.218.62.116' ]
范例4:
// Node.js program to demonstrate the
// dnsPromises.resolve() method
// Accessing promises object from dns module
const dns = require('dns');
const dnsPromises = dns.promises;
// Set the rrtype for dnsPromises.resolve() method
const rrtype = "TXT";
// Calling dnsPromises.resolve() method
dnsPromises.resolve('geeksforgeeks.org', rrtype).then((res) => {
console.log(res);
});
输出:
(node:12752) ExperimentalWarning:The dns.promises API is experimental [ [ 'v=spf1 include:amazonses.com include:_spf.google.com -all' ], [ 'fob1m1abcdp777bf2ncvnjm08n' ] ]
注意:上面的程序将通过使用node index.js
命令。
参考: https://nodejs.org/api/dns.html#dns_dnspromises_resolve_hostname_rrtype
相关用法
注:本文由纯净天空筛选整理自anwesha0107大神的英文原创作品 Node.js | dnsPromises.resolve() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。