Skip to content
Myr

More session docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent cbdd907 · Browse files · 1 file+27 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+27 added−0 removed

  • other:+27 added−0 removed

1 file+27 added−0 removed

modified:docs/guide.md+27 added
409 unmodified lines
410 var RedisStore = require('connect-redis');411 app.use(express.cookieDecoder());412 app.use(express.session({ store: new RedisStore }));413414Now the _req.session_ and _req.sessionStore_ properties will be accessible to all routes and subsequent middleware. Properties on _req.session_ are automatically saved on a response, so for example if we wish to shopping cart data:415416 var RedisStore = require('connect-redis');417 app.use(express.cookieDecoder());418 app.use(express.session({ store: new RedisStore }));419 app.use(express.bodyDecoder());420421 app.post('/add-to-cart', function(req, res){422 // Perhaps we posted several items with a form423 // (use the bodyDecoder() middleware for this)424 var items = req.body.items;425 req.session.items = items;426 res.redirect('back');427 });428429 app.get('/add-to-cart', function(req, res){430 // When redirected back to GET /add-to-cart431 // we could check req.session.items && req.session.items.length432 // to print out a message433 if (req.session.items && req.session.items.length) {434 req.flash('info', 'You have %s items in your cart', req.session.items.length);435 }436 res.render('shopping-cart');437 });438439The _req.session_ object also has methods such as _Session#touch()_, _Session#destroy()_, _Session#regenerate()_ among others to maintain and manipulate sessions. For more information view the [Connect Session](http://senchalabs.github.com/connect/session.html) documentation.440441### req.header(key[, defaultValue])442443Get the case-insensitive request header _key_, with optional _defaultValue_:444
398 unmodified lines