I'm following an example in Node.js in Action and I'm having trouble getting it to work. Here's my code:
var connect = require('connect');
var RedisStore = require('connect-redis')(connect);
var app = connect();
app.use(connect.favicon());
app.use(connect.cookieParser('Cereal killin keybored kittty'));
app.use(connect.session({
store: new RedisStore({prefix: 'boom'})
}));
app.use(function (req, res, next) {
console.log(req.session);
if (req.session.views) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<p>views: ' + req.session.views + '</p>');
res.write('<p>expires in: ' + req.session.cookie.maxAge + '</p>');
res.write('<p>httpOnly: ' + req.session.cookie.httpOnly + '</p>');
res.write('<p>path: ' + req.session.cookie.path + '</p>');
res.write('<p>domain: ' + req.session.cookie.domain + '</p>');
res.write('<p>secure: ' + req.session.cookie.secure + '</p>');
res.end();
req.session.views += 1;
} else {
req.session.views += 1;
res.end('Welcome to the session demo. Refresh the page.');
}
}).listen(3000);
No matter how many times I refresh, req.session.views remains equal to 1. Any help would rock!
Try saving the session explicitly after changing it:
I think the only store for which an explicit
save()
isn't required is theMemoryStore
(just checked the example code for Node.js in Action, that also uses theMemoryStore
).