user1030206
user1030206

Reputation: 33

Specify the outgoing IP adress for a client connection in Node.js

Is there a means by which I can specify the outgoing IP address for a client connection when using Node.js?

Upvotes: 3

Views: 1160

Answers (2)

Antoine F.
Antoine F.

Reputation: 393

It is available since 2013.01.11 Version 0.9.6 (Unstable) release, with localAddress option.

var options = {
  hostname: 'www.google.fr',
  localAddress: '192.168.0.2',
};

var req = http.get(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

Upvotes: 0

Andrey Sidorov
Andrey Sidorov

Reputation: 25466

it seems at the moment the only solution is to use _createServerHandle function

var s = new net.Socket({ handle: 
net._createServerHandle(localAddress) }); 
s.connect(port, host, cb); 

see this thread on mailing list.

Upvotes: 2

Related Questions