node.js send http request with temporary /etc/hosts setting

Viewed 1569

/etc/hosts file allows us to specify an ip address for a domain.

For example, if the content of /etc/hosts file looks like this:

127.0.0.1 www.mydomain.com

It means that www.mydomain.com represents 127.0.0.1 when you type www.mydomain.com in the browser URL bar.

Is it possible to specify a domain for an ip address when sending requests with http.get() without modifying /etc/hosts?

I work on a public ubuntu machine where many people get access to /etc/hosts file. So it's best not to modify /etc/hosts.

The options parameter does not support this feature.

And I don't want to use hostile because it changes /etc/hosts.

2 Answers

You should setup your http.Agent config. For example, when using node-request, you can set option.agentOptions with {host: ${ip}}, before which you should judge the request's host and write a host/ip map file in advance.

Related