Open In App

Node.js dnsPromises.getServers() Method

Last Updated : 13 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The dnsPromises.getServers() method is an inbuilt application programming interface of dns module and promises object which is used to get the IP addresses of the current server.

Syntax:

dnsPromises.getServers()

Parameters: This method doesn’t accept any parameters.

Return Value: This method returns an array of IP addresses in RFC 5952 format as configured in DNS resolution for the current host. If a custom port is used then a string will be attached as a port number.

Below examples illustrate the use of dnsPromises.getServers() method in Node.js:

Example 1:




// Node.js program to demonstrate the   
// dnsPromises.getServers() method
  
// Accessing promises object from dns module
const { Resolver } = require('dns').promises;
  
// Calling Resolver constructor
const dnsPromises = new Resolver();
   
// Asynchronous function 
(async function() {
      
    // Address from getServers function
    const addresses = await dnsPromises.getServers();
      
    // Printing  addresses
    console.log(addresses);   
})();


Output:

[ '10.15.13.139', '8.8.8.8' ]

Example 2:




// Node.js program to demonstrate the   
// dnsPromises.getServers() method
  
// Accessing promises object from dns module
const { Resolver } = require('dns').promises;
  
// Calling Resolver constructor
const dnsPromises = new Resolver();
   
// Asynchronous function 
(async function() {
      
    // Address from getServers function
    const addresses = await dnsPromises.getServers();
      
    // Printing each addresses
    addresses.forEach(element => {  
        console.log(element);  
    }); 
})();


Output:

72.28.94.156
2306:2470:3160::8888
72.28.94.156:1053
[2306:2470:3160::8888]:1053

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

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads