title: Programmatically edit unix host file
body: <p>I am currently using a host file to redirect all traffic to certain external host to my machine in order for my (nodejs) application to intercept it.</p>
  
  <pre><code>/etc/hosts: 
  
  127.0.0.1 externalhost.com
  </code></pre>
  
  <p>My application receives all traffic that is supposed to go to <code>externalhost.com</code> and then decides whether to</p>
  
  <p>a) forward the request to the original resource (e.g. a request to <code>externalhost.com/target_B</code>) </p>
  
  <p>b) intercept the request and send back custom response (e.g. a request to <code>externalhost.com/target_A</code>) </p>
  
  <p>However, the original resource is the host I defined in my host file, thus, creating an endless loop.</p>
  
  <p>Is there a way to programmatically disable host redirection? I don't want to write to that file each time due to performance reasons.</p>