Created
June 7, 2019 06:05
-
-
Save phongkt-dev/b0d562c666b8c3a496207fcc5d32b4af to your computer and use it in GitHub Desktop.
This file contains hidden or 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') | |
| var app = express() | |
| app.use(session({ | |
| secret: 'keyboard cat', | |
| 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('/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') | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment