Node.js | dns.getServers() Method

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

Syntax:

dns.getServers()

Parameters: This method does not accept any parameters.

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

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



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// dns.resolveSoa() method
  
// Accessing dns module
const dns = require('dns');
  
// Reading IP adress of the current host
// and printing it to the console
console.log(dns.getServers());

chevron_right


Output:

[ '2402:3a80:1103:a055::34', '192.168.43.1' ]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// dns.resolveSoa() method
  
// Accessing dns module
const dns = require('dns');
  
// Reading IP adress of the current host
// and printing it to the console
res = dns.getServers();
  
res.forEach(element => { 
    console.log(element); 
});

chevron_right


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_dns_getservers

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.