Systems Rebooter
Systems Rebooter

Reputation: 1399

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

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' ]

Upvotes: 2

Views: 12430

Answers (2)

AliSh
AliSh

Reputation: 10619

You can use following command:

npm config set dns 8.8.8.8

Upvotes: 0

wargre
wargre

Reputation: 4753

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

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

Upvotes: 8

Related Questions