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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
});

chevron_right


Output:

[ 'ns-869.awsdns-44.net',
  'ns-245.awsdns-30.com',
  'ns-1520.awsdns-62.org',
  'ns-1569.awsdns-04.co.uk' ]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);   
})();

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
});

chevron_right


Output:

[ '34.218.62.116' ]

Example 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
});

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.