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

倾然丶 夕夏残阳落幕 提交于 2020-01-01 08:52:48

问题


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.

I was trying to use this npm module. But this resulted in the same situation. how to fix this session issue.

sticky(http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
}););

回答1:


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);



回答2:


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'));
});


来源:https://stackoverflow.com/questions/23991413/how-to-use-sticky-session-with-cluster-in-express-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!