我有3个node.js应用程序(A,B,C)。 A是一个入口点,我喜欢所有到达此入口点的请求被重定向到B或C(取决于请求中参数的值)。几个node.js进程之间的通信
目前我正在做一个res.redirect在A(我使用expressjs框架)。这工作正常,但它不是真正透明(我可以从外面看到原始请求已被重定向)。
为了解决这个问题,我将B和C监听套接字而不是端口号,但我不知道如何将请求重定向到B或C所使用的套接字。
关于如何拥有2 node.js进程通过套接字进行通信?
**更新**
I have changed the code of A to use node-proxy:
app.all('/port/path/*', function(req, res){
// Host to proxied to
var host = 'localhost';
// Retrieve port and build new URL
var arr_url = req.url.split('/');
var port = arr_url[1];
var new_url = '/' + arr_url.slice(2).join('/');
console.log("CURRENT URL:" + req.url); // url is /5000/test/...
console.log("NEW URL :" + new_url); // url is /test/...
// Change URL
req.url = new_url;
var proxy = new httpProxy.HttpProxy();
proxy.proxyRequest(req, res, {
host: host,
port: port,
enableXForwarded: false,
buffer: proxy.buffer(req)
});
console.log("Proxied to " + host + ':' + port + new_url);
// For example: the url is localhost:10000/5000/test
// the log tells me 'Proxied to localhost:5000/test' => that is correct
// But... I do not get any return
// If I issue 'curl -XGET http://localhost:5000/test' I get the return I expect
});
在这个任何明显的错误?
A,B和C都是Web服务吗? –
是的,有所有的网络服务,但我不想暴露B和C到外面。 – Luc
Josh有正确的答案:将B和C绑定到'localhost',以便它们不暴露于外部世界,然后将A写为反向代理。 (或者使用nginx,如果A只是一个总机)。 –