2

I want to force my node js app to download through a specific interface or IP address.

In Linux with wget I can do this with something like this:

wget --bind-address=192.168.21.21 http://example.com

or similarly with curl:

curl --interface 192.168.21.21 --ipv4 http://example.com

I am currently using the request package but I can't see a similar option and could change from this if I had to.

How can I bind my download to an IP address or interface with node js request?

Edit: I have seen the questions in the comments but they don't seem to address my question of how to do this in request and I am not using expressjs as my app doesn't have a server/web presence.

user1464409
  • 1,032
  • 5
  • 18
  • 31
  • 1
    I believe you are referring to this SO question http://stackoverflow.com/questions/9986220/bind-expressjs-to-a-specific-ip-address – Barry Dick Dec 17 '16 at 08:33
  • 1
    Possible duplicate of [How to specify a network interface when making net requests from Node.js?](http://stackoverflow.com/questions/13602066/how-to-specify-a-network-interface-when-making-net-requests-from-node-js) – Tomalak Dec 17 '16 at 08:38
  • 1
    Please see my edit, I am looking for a solution with node.js request preferably. If my question is answered by the one referred to by Barry Dick I am unclear as to how to implement it. – user1464409 Dec 17 '16 at 08:59
  • 1
    Download what? the App itself? Download a file? If you are using request, most probably you need a proxy. (https://github.com/nodejitsu/node-http-proxy) – Hosar Dec 17 '16 at 10:20
  • Sorry I haven't made that very clear. My app downloads a file. I am trying to download the file using the request module indicated in my question. I am now off to look at the node-http-proxy module you have offered to me as help. – user1464409 Dec 17 '16 at 11:42

1 Answers1

5

I had the same requirement and it turns out it's really easy to do. Just use the localAddress option in request (it's the same option as in the http module):

(async () => {

  let request = require('request-promise-native'); // (a promisified version of request module)

  // This prints your machine's IP
  console.log( await request({ uri: 'https://api.ipify.org' }) );

  // This prints '***.***.***.***'
  console.log( await request({ uri: 'https://api.ipify.org', localAddress: '***.***.***.***' }) );

})();

(Replace ***.***.***.*** with your new static IP)

Note that you will of course need to have already set up your static IP and have configured it in your OS. In case it helps anyone, with Ubuntu 16.04 you'd just add this to the end of your /etc/network/interfaces file:

auto ens3:0
iface ens3:0 inet static
  address ***.***.***.***
  netmask 255.255.254.0

(Again, replace ***.***.***.*** with your IP) More details here.