Last active
July 5, 2021 12:52
-
-
Save enesakar/ffdc814b6b16fbe613e74f542a575a61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
var parseurl = require('parseurl') | |
var session = require('express-session') | |
const redis = require('redis') | |
var RedisStore = require('connect-redis')(session) | |
var client = redis.createClient ({ | |
// REPLACE HERE | |
}); | |
var app = express() | |
app.use(session({ | |
store: new RedisStore({ client: client }), | |
secret: 'forest squirrel', | |
resave: false, | |
saveUninitialized: true | |
})) | |
app.use(function (req, res, next) { | |
if (!req.session.views) { | |
req.session.views = {} | |
} | |
// get the url pathname | |
var pathname = parseurl(req).pathname | |
// count the views | |
req.session.views[pathname] = (req.session.views[pathname] || 0) + 1 | |
next() | |
}) | |
app.get('/', function (req, res, next) { | |
res.send('you viewed this page ' + req.session.views['/'] + ' times') | |
}) | |
app.get('/foo', function (req, res, next) { | |
res.send('you viewed this page ' + req.session.views['/foo'] + ' times') | |
}) | |
app.get('/bar', function (req, res, next) { | |
res.send('you viewed this page ' + req.session.views['/bar'] + ' times') | |
}) | |
app.listen(8080, function () { | |
console.log('Example app listening on port 8080!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment