12

我尝试在 NodeJS 中使用 Redis 的标准会话持久性:

var express = require('express');
var RedisStore = require('connect-redis')(express);
var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({ 
    secret: "keyboard cat", 
    store: new RedisStore, 
    key: 'sid' 
  }));
});


app.get('/', function (req, res) {
  if (req.session.isValid) {
    console.log("There is an existing session.");
  }
  else {
    req.session.isValid = true;
    console.log("New session.");
    console.log('Old session ID: ' + req.header('Cookie'));
    console.log('New session ID: ' + req.session.id);
  }

  res.render('index', {'title': s});
});
app.listen(4000);

理论上我应该看到“新会话”这一行。网站站点的所有后续调用都应导致“存在现有会话”。不幸的是,每次通话都会重新生成一个新的会话 ID。浏览器中的 cookie 工作正常,内容正确传输,我可以在

req.header('Cookie')

这是控制台日志的样子:

[app.js] New session.
[app.js] Old session ID: undefined
[app.js] New session ID: nuoHKZj2j0AoRkvqT4xE5h6W.zF+DNv2rzr3kpeO2IyD7sa4xdamFQMugjfQvY6OYymE
[app.js] New session.
[app.js] Old session ID: sid=neLUc5PXxPoj1yFqukerv49x.BHzYKiuAfFSNHKd4fCAkv8wNwZO%2FxykJPN5R5tjAlQc
[app.js] New session ID: FvuzjnXvchCkmVqsq5mrodL2.5YlT3InfTbvOwEUc0dNpPLT77tcdJpNuhbFGVYkLneQ
[app.js] New session.
[app.js] Old session ID: sid=pFbyVdlNZXtF5vZ35CW9sfmq.nJ1RBjJu59iUJJjmZv9TCYiYLcvycme%2BJh8sQC6%2FzEE
[app.js] New session ID: KdPhwqwwgmOnPZEuVapy7EJe.I0TGT9HSSQQSporwCNsxl11rXDxR/ysjTeZb0lD5uwI

同时在redis-cli中运行“MONITOR”命令时得到以下输出:

sess:nuoHKZj2j0AoRkvqT4xE5h6W.zF+DNv2rzr3kpeO2IyD7sa4xdamFQMugjfQvY6OYymE
*2
$3
get
$73
sess:Q6Z06GL4hdRytKA2MToCIgVw.JWxImSB/m20Urn+IYMQqnNqfQp4ygAESiyBLORn3Iuo
*2
$3
get
$73
sess:neLUc5PXxPoj1yFqukerv49x.BHzYKiuAfFSNHKd4fCAkv8wNwZO/xykJPN5R5tjAlQc
*2
$3
get
$73
sess:FvuzjnXvchCkmVqsq5mrodL2.5YlT3InfTbvOwEUc0dNpPLT77tcdJpNuhbFGVYkLneQ 
*2 
$3
get
$73
sess:zvCWdwzowgAfl6jH8m0D31vL.b5tK5VZUJtPHrdvH09A/hjhjoOg6bT0CmAcWWRf99SI
*2
$3
get
$73
sess:pFbyVdlNZXtF5vZ35CW9sfmq.nJ1RBjJu59iUJJjmZv9TCYiYLcvycme+Jh8sQC6/zEE
*2
$3
get
$73
sess:KdPhwqwwgmOnPZEuVapy7EJe.I0TGT9HSSQQSporwCNsxl11rXDxR/ysjTeZb0lD5uwI
*2
$3
get
$73
sess:r793eaJyOnaq2RNyw1Hmpuwv.xnonbOlaWEAlpz+LDg0SHcUeAa0sbjyw0oIcwFmlX0w

当我使用 MemoryStore 而不是 RedisStore 时,一切都按预期工作。

有任何想法吗?

4

3 回答 3

0

我有类似的问题。我的会话根据每个请求重新生成。我通过在进行开发的虚拟机上设置适当的日期来解决它。

于 2012-04-24T20:19:58.707 回答
0

尝试这个..

let redisStore,redisClient;
const redis = require('redis');
redisStore = require('connect-redis')(session); 

redisClient = redis.createClient();

redisClient.on('error', (err) => {
    console.log('Error: ', err);
});

app.use(session({
    store   : new redisStore({ host: 'localhost', port: 'port', client: redisClient }),
    name    : 'session_name', 
    resave  : false,
    saveUninitialized: false,
    rolling : true,
}));

注意:redis 不支持 window/localhost。

于 2020-12-31T09:21:54.620 回答
-1

当每个请求都重新生成会话时,我遇到了同样的问题。原因是“静态”中间件被放置在“会话”下面。所以,一定要按正确的顺序放置中间件,即:

...
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.session({ store: new RedisStore({ prefix: 'sid_' }) }));
app.use(app.router)
app.get ...

“express.session”中间件应该在“静态”下。

于 2014-01-13T15:19:54.767 回答