How to load balance(bouncy) between two same hosts in node.js

I'm having problem with bouncing between two same hosts but different ports.

So I use module bouncy in node.js, I have two applications host on same host(localhost) but listening different ports.

I'm using a routes.json

{ "localhost" : 8001, "localhost" : 8000 }

and point the bouncy on command-line.

But it seems like it doesn't work with two same hosts? Only the host with port 8000 works...

Any solution for this? Thanks, in advance

I think you misunderstand the point of routes.json: The keys in the hash represent virtual host names.

It could work with domain names, for example:

{ "dev.xx.com" : 8001, "prod.xx.com" : 8000 }

The file-based config works by inspecting req.headers.host to decide where to route your traffic.

Are you trying to load balance two instances of the same app on "localhost"? In that case you should look at the cluster library built into node: http://nodejs.org/api/cluster.html

EDIT:

Both cluster and bouncy can be used to load balance requests for you. If you use bouncy, you will have to write a simple round-robin scheme yourself (can't use routes.json), cluster has it built-in.

Another disadvantage of bouncy (for your specific use case) is that bouncy does no monitoring of the servers it bounces requests to. You'll have to add logic to bouncy to check whether both servers are responsive. Again, this is all built into cluster (it runs the servers as child processes and can restart them if they go down.) Cluster can also be used to safely restart the child servers with new code without losing any requests.

All in all: Bouncy is OK if you want to use it as a proxy, but cluster is made to load-balance.