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
// 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
Recommended Posts:
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs | DNS
- Web-Socket in NodeJS
- Nodejs | Jimp
- Nodejs | Web Crawling using Cheerio
- Dockerizing a simple Nodejs app
- Encrypting Data in NodeJS
- Difference between NodeJS and AngularJS
- Nodejs – Connect Mysql with Node app
- Cowsay in Nodejs using Requests library
- Signup Form Using Nodejs and MongoDB
- NodeJs - Handling invalid routes
- Nodejs - Connect MongoDB with Node app using MongooseJS
- NodeJS | Building simple REST API in express
- Nodejs | authentication using Passportjs and passport-local-mongoose
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.



