Open In App

Node.js dns.lookup() Method

The dns.lookup() method is an inbuilt application programming interface of the dns module which is used to resolve IP addresses of the specified hostname for given parameters into the first found A (IPv4) or AAAA (IPv6) record.

Syntax:



dns.lookup( hostname, options, callback )

Parameters: This method has three parameters as mentioned above and described below:

address: "fd00:0:13:13::22da:3e74" family: IPv6

Example 2:




// Node.js program to demonstrate the 
// dns.lookup() method 
   
// Accessing dns module
const dns = require('dns');
  
// Setting options for dns.lookup()
// method, all as true
const options = {
    all:true,
};
  
// Calling dns.lookup() for hostname
//  geeksforgeeks.org and displaying
// them in console as a callback
dns.lookup('geeksforgeeks.org', options, (err, addresses) =>
        console.log('addresses: %j', addresses));

Output:

addresses: [
    {"address":"34.218.62.116","family":4},
    {"address":"fd00:0:13:13::22da:3e74","family":6}
]

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

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


Article Tags :