Skip to content
Myr

Regenerated guide

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 4bc8a86 · Browse files · 2 files+65 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

2 files+65 added−0 removed

  • other:+65 added−0 removed

2 files+65 added−0 removed

modified:docs/guide.1+37 added
775 unmodified lines
776.fi777.778.IP "" 0779.780.P781Now the \fIreq\.session\fR and \fIreq\.sessionStore\fR properties will be accessible to all routes and subsequent middleware\. Properties on \fIreq\.session\fR are automatically saved on a response, so for example if we wish to shopping cart data:782.783.IP "" 4784.785.nf786787var RedisStore = require(\'connect\-redis\');788app\.use(express\.cookieDecoder());789app\.use(express\.session({ store: new RedisStore }));790app\.use(express\.bodyDecoder());791792app\.post(\'/add\-to\-cart\', function(req, res){793 // Perhaps we posted several items with a form794 // (use the bodyDecoder() middleware for this)795 var items = req\.body\.items;796 req\.session\.items = items;797 res\.redirect(\'back\');798});799800app\.get(\'/add\-to\-cart\', function(req, res){801 // When redirected back to GET /add\-to\-cart802 // we could check req\.session\.items && req\.session\.items\.length803 // to print out a message804 if (req\.session\.items && req\.session\.items\.length) {805 req\.flash(\'info\', \'You have %s items in your cart\', req\.session\.items\.length);806 }807 res\.render(\'shopping\-cart\');808});809.810.fi811.812.IP "" 0813.814.P815The \fIreq\.session\fR object also has methods such as \fISession#touch()\fR, \fISession#destroy()\fR, \fISession#regenerate()\fR among others to maintain and manipulate sessions\. For more information view the Connect Session \fIhttp://senchalabs\.github\.com/connect/session\.html\fR documentation\.816.817.SS "req\.header(key[, defaultValue])"818Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:819.820.IP "" 4
711 unmodified lines
modified:docs/guide.html+28 added
702 unmodified lines
703app.use(express.cookieDecoder());704app.use(express.session({ store: new RedisStore }));705</code></pre>706707<p>Now the <em>req.session</em> and <em>req.sessionStore</em> properties will be accessible to all routes and subsequent middleware. Properties on <em>req.session</em> are automatically saved on a response, so for example if we wish to shopping cart data:</p>708709<pre><code>var RedisStore = require('connect-redis');710app.use(express.cookieDecoder());711app.use(express.session({ store: new RedisStore }));712app.use(express.bodyDecoder());713714app.post('/add-to-cart', function(req, res){715 // Perhaps we posted several items with a form716 // (use the bodyDecoder() middleware for this)717 var items = req.body.items;718 req.session.items = items;719 res.redirect('back');720});721722app.get('/add-to-cart', function(req, res){723 // When redirected back to GET /add-to-cart724 // we could check req.session.items &amp;&amp; req.session.items.length725 // to print out a message726 if (req.session.items &amp;&amp; req.session.items.length) {727 req.flash('info', 'You have %s items in your cart', req.session.items.length);728 }729 res.render('shopping-cart');730});731</code></pre>732733<p>The <em>req.session</em> object also has methods such as <em>Session#touch()</em>, <em>Session#destroy()</em>, <em>Session#regenerate()</em> among others to maintain and manipulate sessions. For more information view the <a href="http://senchalabs.github.com/connect/session.html">Connect Session</a> documentation.</p>734735<h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>736737<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>738
456 unmodified lines