开发者

Communication between several node.js process

I have 3 node.js application (A, B, C). A is an entry point and I like all the request that gets to this entry point to be redirected toward B or C (depending upon the value of a parameter in the request).

Currently I'm doing so with a res.redirect in A (I'm using expressjs framework). That's working fine except that it's not really transparent (I can see from the outside that the original request has been redirected).

To solve this, I will have B and C listen on socket instead of port number but I do not know how to have A redirecting the request to the sockets used by B or C.

Any idea on how to have 2 node.js process communicating via sockets ?

** UPDATE **

I have changed the code of A to use node-proxy:  

app.all('/port/path/*', function(req, res){

    // Host to proxied to
    var host = 'localhos开发者_如何学JAVAt';

    // 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

});

Any obvious mistake in this ?


You're on the right track with having the other processes listen on different ports. What you're talking about is called a reverse proxy. If its http, its pretty straightforward to get this going with node-http-proxy:

https://github.com/nodejitsu/node-http-proxy

You want to set something up like a proxy table:

var options = {
  router: {
    'foo.com/baz': '127.0.0.1:8001',
    'foo.com/buz': '127.0.0.1:8002',
    'bar.com/buz': '127.0.0.1:8003'
  }
};


I just put in an issue for for the node-http-rpoxy module here https://github.com/nodejitsu/node-http-proxy/issues/104

Apparently it does in fact work with unix sockets right now, but like this (subject to change).

var options = {
    router: {
        'foo.com/baz': ':/tmp/nodeserver.sock',
    }
};

All it needs is a colon to use the socket path as the port value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜