Node override request IP resolution

2019-04-25 16:56发布

问题:

I'm struggling to find the correct terminology to accurately phrase this problem, but I'm gonna give it my best shot:

In node.js, is there a way to manually override the IP address when making an HTTP request (e.g. request some-domain.com/whatever and instead of resolving the IP address through DNS, manually provide some IP address 1.2.3.4) ?

This would, effectively speaking, be the equivalent of setting 1.2.3.4 some-domain.com in /etc/hosts

回答1:

There is a tiny module that does exactly that: evil-dns.

evilDns.add('foo.com', '1.2.3.4');


回答2:

I'd suggest looking at Nodejs's doc on the DNS API (https://nodejs.org/api/dns.html). You can modify the OS host file and use dns.lookup() to pull from the host file and not do a DNS query.

Not sure if you are trying to avoid modifying the host file?