Allow dynamic mounting with `.use`.
[express.git] / examples / session / index.js
blob3629914d1eaab15d4020723c792fd1c3ae1b2992
2 // first:
3 // $ npm install redis
4 // $ redis-server
6 var express = require('../..');
7 var cookieParser = require('cookie-parser');
8 var session = require('express-session');
10 var app = express();
12 // Required by session() middleware
13 // pass the secret for signed cookies
14 // (required by session())
15 app.use(cookieParser('keyboard cat'));
17 // Populates req.session
18 app.use(session());
20 app.get('/', function(req, res){
21 var body = '';
22 if (req.session.views) {
23 ++req.session.views;
24 } else {
25 req.session.views = 1;
26 body += '<p>First time visiting? view this page in several browsers :)</p>';
28 res.send(body + '<p>viewed <strong>' + req.session.views + '</strong> times.</p>');
29 });
31 app.listen(3000);
32 console.log('Express app started on port 3000');