How to make a distributed node.js application?

前端 未结 4 1609
难免孤独
难免孤独 2021-01-30 01:22

Creating a node.js application is simple enough.

var app = require(\'express\')();
app.get(\'/\',function(req,res){
    res.send(\"Hello world!\");
});
         


        
4条回答
  •  野的像风
    2021-01-30 02:17

    The basic way to use multiple machines is to put them behind a load balancer, and point all your traffic to the load balancer. That way, someone going to http://my_domain.com, and it will point at the load balancer machine. The sole purpose (for this example anyways; in theory more could be done) of the load balancer is to delegate the traffic to a given machine running your application. This means that you can have x number of machines running your application, however an external machine (in this case a browser) can go to the load balancer address and get to one of them. The client doesn't (and doesn't have to) know what machine is actually handling its request. If you are using AWS, it's pretty easy to set up and manage this. Note that Pascal's answer has more detail about your options here.

    With Node specifically, you may want to look at the Node Cluster module. I don't really have alot of experience with this module, however it should allow you to spawn multiple process of your application on one machine all sharing the same port. Also node that it's still experimental and I'm not sure how reliably it will be.

提交回复
热议问题