Node.js dnsPromises.resolve() Method
The dnsPromises.resolve() method is an inbuilt application programming interface of the promises object of dns module which is used to resolve hostname into an array of the resource records.
Syntax:
dnsPromises.resolve( hostname, rrtype )
Parameters: This method has two parameters as mentioned above and described below:
- hostname: This parameter specifies a string which denotes the hostname to be resolved.
- rrtype: It specifies resource record type. Its default value is ‘A’. The values are from ‘A’, ‘AAAA’, ‘ANY’, ‘CNAME’, ‘MX’, ‘TXT’, ‘NS’, ‘NAPTR’, ‘PTR’, ‘SOA’, ‘SRV’.
- A: IPv4 address
- AAAA: IPv6 address
- ANY: Any records
- CNAME: Canonical name records
- MX: Mail exchange records
- NAPTR: Name authority pointer records
- NS: Name server records
- PTR: Pointer records
- SOA: Start of authority records
- SRV: Service records
- TXT: Text records
Return Value: This method returns error, records.
Below examples illustrate the use of dnsPromises.resolve() method in Node.js:
Example 1:
const dns = require( 'dns' );
const dnsPromises = dns.promises;
const rrtype= "NS" ;
dnsPromises.resolve( 'geeksforgeeks.org' , rrtype).then((res) => {
console.log(res);
});
|
Output:
[ 'ns-869.awsdns-44.net',
'ns-245.awsdns-30.com',
'ns-1520.awsdns-62.org',
'ns-1569.awsdns-04.co.uk' ]
Example 2:
const dns = require( 'dns' );
const dnsPromises = dns.promises;
const rrtype = "MX" ;
(async function () {
const records = await dnsPromises.resolve( 'geeksforgeeks.org' , rrtype);
console.log( "from async: " );
console.log(records);
})();
|
Output:
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 }
]
Example 3:
const dns = require( 'dns' );
const dnsPromises = dns.promises;
const rrtype = "A" ;
dnsPromises.resolve( 'geeksforgeeks.org' , rrtype).then((res) => {
console.log(res);
});
|
Output:
[ '34.218.62.116' ]
Example 4:
const dns = require( 'dns' );
const dnsPromises = dns.promises;
const rrtype = "TXT" ;
dnsPromises.resolve( 'geeksforgeeks.org' , rrtype).then((res) => {
console.log(res);
});
|
Output:
(node:12752) ExperimentalWarning: The dns.promises API is experimental
[ [ 'v=spf1 include:amazonses.com include:_spf.google.com -all' ],
[ 'fob1m1abcdp777bf2ncvnjm08n' ] ]
Note: The above program will compile and run by using the node index.js
command.
Reference: https://nodejs.org/api/dns.html#dns_dnspromises_resolve_hostname_rrtype
Last Updated :
13 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...