Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 412
Vicary Avatar asked Nov 28 '12 09:11

Vicary


People also ask

How do I create a socket server listening for connection on given path?

socket.connect(path[, connectListener]) Opens the connection for a given socket. If port and host are given, then the socket will be opened as a TCP socket, if host is omitted, localhost will be assumed. If a path is given, the socket will be opened as a Unix socket to that path.

How does node js handle request?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.


1 Answers

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.

like image 180
jpillora Avatar answered Sep 19 '22 20:09

jpillora