How to specify a network interface when making net requests from Node.js?

Vicary picture Vicary · Nov 28, 2012 · Viewed 9.5k times · Source

In either http.request or net.connect, is there an option that I can specify a network interface to initiate a connection?

EDIT: AFAIK in OS level I can specify address level, or load balancing into routing tables. But the way of interface choosing in my software is more than that, I wanna know if I can do that in codes.

Answer

jpillora picture jpillora · Jan 8, 2014

Node has this built in:

http://nodejs.org/api/net.html#net_net_connect_options_connectionlistener

http://nodejs.org/api/http.html#http_http_request_options_callback

See localAddress, just set that to the IP of the interface you'd like to use.