Node.js dns.resolveAny() Method

The dns.resolveAny() method is an inbuilt application programming interface of the dns module which is used to resolve all records (i.e. ‘ANY’ or ‘*’) for the specified hostname using DNS protocol.

Syntax:

dns.resolveAny( hostname, callback )

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.
  • callback: It specifies a function to be called after DNS resolution of the hostnames.
    • error: It specifies error if generated.
    • ret: It is an object that signifies the returned records for the hostname.

Return Value: This method returns error, records through callback function. These data are passed as parameters to the callback function.

Records that may be returned are:

  • 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

Below examples illustrate the use of dns.resolveAny() method in Node.js:

Example 1:




// Node.js program to demonstrate the   
// dns.resolveAny() method
  
// Accessing dns module
const dns = require('dns');
  
// Calling dns.resolveAny() method for 
// hostname w3wiki.net and displaying
// them in console as a callback
dns.resolveAny('w3wiki.net', (err, 
    ret) => console.log('records: %j', ret));


Output:

records: [
    {"value":"ns-1520.awsdns-62.org","type":"NS"},
    {"value":"ns-1569.awsdns-04.co.uk","type":"NS"},
    {"value":"ns-245.awsdns-30.com","type":"NS"},
    {"value":"ns-869.awsdns-44.net","type":"NS"}
]

Example 2:




// Node.js program to demonstrate the   
// dns.resolveAny() method
  
// Accessing dns module
const dns = require('dns');
  
// Calling dns.resolveAny() method for 
// hostname localhost and displaying
// them in console as a callback
dns.resolveAny('localhost', (err, 
    ret) => console.log('records: %j', ret));


Output:

records: [
    {"address":"127.0.0.1", "ttl":0, "type":"A"}
]

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/dns.html#dns_dns_resolveany_hostname_callback