How to cluster Node.js app in multiple machines

前端 未结 2 1718
旧巷少年郎
旧巷少年郎 2021-01-31 06:07

I am using Express js and Node-cluster for taking the advantage of clustering I am also using PM2 for process and memory management. For a single machine, it is working fine, bu

2条回答
  •  逝去的感伤
    2021-01-31 06:25

    Horizontal scaling for node can be done in multiple ways :

    1. npm install http-proxy

      var proxyServer = require('http-proxy');
      var port = parseInt(process.argv[2]);
      var servers = [
        {
          host: "localhost",
          port: 8081
        },
        {
          host: "localhost",
          port: 8080
        }
      ];
      
      proxyServer.createServer(function (req, res, proxy) {
        var target = servers.shift();
      
        proxy.proxyRequest(req, res, target);
        servers.push(target);
      }).listen(port); 
      
    2. nginx

    for more detail please check the below URL Horizontal scaling for node js

提交回复
热议问题