I am pulling my hair out here, I've done Express session in the past, I've followed every Stackoverflow post on related issue. No luck.
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon(__dirname + '/public/favicon.ico'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'ponies' }));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/api/characters', function(req, res) {
// Initially false/undefined
if (req.session.hasViewed) {
return res.send(allCharacters.slice(req.session.currentCounter, req.session.currentCounter+2));
}
req.session.hasViewed = true;
req.session.currentCounter = counter;
res.send(allCharacters.slice(counter, counter + 2));
counter = counter + 2;
});
Why do req.sessionhasViewed and req.session.currentCounter are always undefined? Actually, not always, sometimes on the 6th or 7th tries the session object kicks in and it works. Which is even worse than never working.
Background: The view should display 2 characters initially. If the user presses F5 to refresh the browser, the same two characters should be displayed again, until he/she clicks on one of them. Clearing the session object is performed in another function.
Update: I was pressing F5 repeatedly; when you see Personal log message that's when the session kicks-in.

Update 2: Ok, so I don't know if this makes any difference, but the code above is located in /api/characters. But the actual URL when that API is loaded at "/" route via a Backbone Router that instantiates a new view for that route.