Skip to content
Related Articles

Related Articles

Nodejs | DNS | setServers()
  • Last Updated : 15 Apr, 2019
GeeksforGeeks - Summer Carnival Banner

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






// 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  

Output:

Example 2: Without Callback Function




// 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  

Output:

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :