Nodejs | DNS | setServers()

The setServers() function is an inbuilt function in node.js Resolver class which is used to set the IP address and port of servers to be used when performing DNS resolution. The argument of the servers is an array of RFC 5952 formatted addresses. An error will be thrown if an invalid address is provided with the help of the callback function.

Syntax:

dns.setServers(array );

Example:



dns.setServers([
  '4.4.4.4',
  '[2001:4860:4860::8888]'
]);

Parameters:This function accept one parameter as an array string of format rfc5952.
Return Value: This function returns undefined object on success.

Example 1: With Callback function

filter_none

edit
close

play_arrow

link
brightness_4
code

// Include 'dns' module and create its object 
const { Resolver } = require('dns');
const resolver = new Resolver();
  
// Set Servers
resolver.setServers(['11.11.1.11']);
  
// Resolve IPv4 Addresses using resolve4 
//function of Resolver class with callback function
resolver.resolve4('geeksforgeeks.org', function(err){
    if (err) throw err;
    console.log("Server Address set by setServer function is "
                 +resolver.getServers());
});
  
// Execute using $ node  

chevron_right


Output:

Example 2: Without Callback Function

filter_none

edit
close

play_arrow

link
brightness_4
code

// Include 'dns' module and create its object 
const { Resolver } = require('dns');
const resolver = new Resolver();
  
// Set Servers
resolver.setServers(['24.4.4.24']);
  
// Call to a Resolver function without callback function
console.log(resolver.getServers());
  
// Execute using $ node  

chevron_right


Output:

Reference: https://nodejs.org/docs/latest-v9.x/api/dns.html#dns_dns_setservers_servers



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.