How to use sticky-session with cluster in express - node js

前端 未结 2 1433
不知归路
不知归路 2021-02-20 05:30

I created a cluster depending app with reference to this question

But I started facing issues in session handling. how to use sticky-session in express js with cluster.<

相关标签:
2条回答
  • 2021-02-20 05:38

    Finally found solution just try this code. Its maintain sticky as well as it uses all the cpus [ process ] for other clients. You can use express cluster sticky session using following code. You can get sticky-session here https://github.com/indutny/sticky-session

    var http = require('http');
    var cluster = require('cluster'); // Only required if you want the worker id
    var sticky = require('sticky-session');
    var express = require('express');
    var app = express();
    
    app.get('/', function (req, res) {
        console.log('worker: ' + cluster.worker.id);
        res.send('Hello World!');
    });
    
    
    var server = http.createServer(app);
        sticky.listen(server,3000);
    
    0 讨论(0)
  • 2021-02-20 06:00

    It has nothing to do with Express.

    You just forgot the listen() on the sticky function.

    sticky(
      http.createServer(app).listen(app.get('port'), function () {
          console.log('Express server listening on port ' + app.get('port'));
      });
    ).listen(app.get('port'),function() {
      console.log('Sticky server started on port' + app.get('port'));
    });
    
    0 讨论(0)
提交回复
热议问题