12

I tried to use the standard session persistance with Redis in NodeJS:

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

In theory I should see the line "New session." once and all subsequent calls of the website site should lead to "There is an existing session". Unfortunately on every call a new session ID is regenerated. The cookie in the browser is working fine, the content is correctly transmitted and I can see it in

req.header('Cookie')

This is what the console log looks like:

[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

At the same time I get the following output when running the "MONITOR" command in redis-cli:

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

When I use MemoryStore insteand of RedisStore, everything works like expected.

Any ideas?

Huy-Anh Hoang
  • 656
  • 5
  • 19
Raph
  • 121
  • 1
  • 4

3 Answers3

0

I had similar issue. My session was regenerated upon every request. I solved it by setting proper date on virtual machine where development takes place.

nrph
  • 305
  • 7
  • 17
0

Try this..

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

Note: redis can't support in window/localhost.

kiran_ray
  • 199
  • 8
-1

I had the same issue when sessions had been regenerated on every request. The reason was that the 'static' middleware was placed below 'session' one. So, be sure to place middlewares in proper order, i.e:

...
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' middleware should be under 'static'.