Is it possible with a nodejs server to have it receive a message on one network interface and then output an HTTP request on a different network interface?
The scenario: I have a device on a local network and I want the server to be able to receive data from that and format it into a request to update a third party web app. I have confirmed that I am receiving the local message correctly, and I have confirmed that the format of the HTTP request should work using fiddler, but somewhere between the two parts I am having an issue. I get a "socket hang up" error at the moment.
Thanks in advance
Edit:
Below is my server code. I have specified IP addresses for the incoming and outgoing messages, but I still get a socket hang up error. Can anyone spot anything else which could be causing this issue?
I am wondering if I need to do something differently for a secure connection? (it is a https URL)
var url = require("url");
var http = require('http');
http.createServer(function (request,response) {
var theUrl = url.parse(request.url);
console.log(theUrl);
var timeline = { // Data to be sent to external web service
channel: "#test",
username: "Slash",
icon_emoji: ":smirk_cat:",
text: "Timeline Started: " + theUrl.path
};
var data = JSON.stringify(timeline)
var options = {
host: "example.com",
path: "/path/to/web/service",
localAddress: '172.20.1.53', // IP address of outgoing NIC
method: 'POST',
port: "443",
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data),
}
};
var req = http.request(options, function(res){
res.on('data', function(resp) {
console.log(resp)
});
});
req.on('error', function(err) {
console.log("ERROR: " + err.message);
});
req.write(data);
console.log(req);
req.end();
}).listen(801, '172.30.2.92'); // IP address of incoming NIC
I have managed to fix this issue, I just needed to change var req = http.request to var req = https.request
Yes, you can specify the interface by the assigned IP address.
For servers you specify it in listen()
like so:
server.listen(8000, '192.168.100.14');
For outgoing HTTP requests you can specify it as localAddress
like so:
http.request({
localAddress: '192.168.5.27',
// ...
}, ...);