nodejs服務器是否可以讓它在一個網絡接口上接收消息,然後在不同的網絡接口上輸出HTTP請求?Nodejs服務器與兩個網絡接口卡進行通信
該場景: 我在本地網絡上有一個設備,我希望服務器能夠接收來自該設備的數據並將其格式化爲更新第三方Web應用程序的請求。 我已確認我正確接收本地消息,並且已確認HTTP請求的格式應該使用提琴手,但在兩個部分之間存在問題。 我現在收到「套接字掛斷」錯誤。
在此先感謝
編輯: 下面是我的服務器代碼。我爲輸入和輸出消息指定了IP地址,但我仍然遇到套接字掛起錯誤。任何人都可以找到可能導致此問題的其他事情嗎?
我想知道如果我需要爲安全連接做不同的事情嗎? (它是一個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
傳出接口由操作系統的IP網絡層路由規則選擇。不過這聽起來像是你的問題與路由無關,而是與web服務器或配置中的其他問題有關。 (你可以使用curl來調試服務器機器上的請求嗎?) – 2014-08-29 14:58:21