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:
// 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);
}); |
Output:
[ 'ns-869.awsdns-44.net', 'ns-245.awsdns-30.com', 'ns-1520.awsdns-62.org', 'ns-1569.awsdns-04.co.uk' ]
Example 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);
})(); |
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:
// 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);
}); |
Output:
[ '34.218.62.116' ]
Example 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);
}); |
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