Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set dns server in node.js resolve query?

Tags:

node.js

dns

I'm trying to set Google DNS server 8.8.8.8 in Node.js resolve query.

What is the correct way to do so? In command line usually we can do the following:

$ nslookup stackoverflow.com 8.8.8.8

Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
Name:   stackoverflow.com
Address: 151.101.1.69
Address: 151.101.65.69
Address: 151.101.129.69
Address: 151.101.193.69

But its not quite clear how to make same approach in Node.js

require('dns').resolve('stackoverflow.com', function (err, addresses) {
  console.log(err, addresses);
});

// => null [ '151.101.1.69', '151.101.65.69', '151.101.129.69', '151.101.193.69' ]

like image 530
Systems Rebooter Avatar asked Sep 14 '25 07:09

Systems Rebooter


1 Answers

From nodejs doc: https://nodejs.org/api/dns.html#dns_dns_setservers_servers

dns.setServers([
   '4.4.4.4',
   '[2001:4860:4860::8888]',
]);
like image 172
wargre Avatar answered Sep 15 '25 21:09

wargre