Node override request IP resolution

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?


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

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

Node's http and https modules take an Agent as an argument, and you can override the dns resolver with the lookup parameter.

const http = require("http");
const https = require("https");


const staticLookup = (ip, v) => (hostname, opts, cb) => cb(null, ip, v || 4)
const staticDnsAgent = (scheme, ip) => new require(scheme).Agent({lookup: staticLookup(ip)})

http.get("http://some-domain.com/whatever", {agent: staticDnsAgent('http', '1.2.3.4')})