Skip to content

Commit

Permalink
refactored cookie-sessions example
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 24, 2012
1 parent 7115193 commit d396761
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions examples/cookie-sessions/index.js
Expand Up @@ -7,34 +7,16 @@ var express = require('../../');

var app = module.exports = express();

/**
* Cookie session middleware using the given cookie `name`.
*
* Here we simply check for a signed cookie of the same name,
* then save the object as JSON on response, again as a signed cookie.
*/

function cookieSessions(name) {
return function(req, res, next) {
req.session = req.signedCookies[name] || {};

res.on('header', function(){
res.signedCookie(name, req.session);
});

next();
}
}
// pass a secret to cookieParser() for signed cookies
app.use(express.cookieParser('manny is cool'));

// for this we need cookie support! this will
// populate req.{signedCookies,cookies}()
// add req.session cookie support
app.use(express.cookieSessions());

app.use(express.cookieParser('manny is cool'));
app.use(cookieSessions('sid'));
// do something with the session
app.use(count);

// do something with our session

// custom middleware
function count(req, res) {
req.session.count = req.session.count || 0;
var n = req.session.count++;
Expand Down

0 comments on commit d396761

Please sign in to comment.