Open In App

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:

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:

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 geeksforgeeks.org and displaying
// them in console as a callback
dns.resolveAny('geeksforgeeks.org', (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


Article Tags :