-
Notifications
You must be signed in to change notification settings - Fork 10
/
session.js
55 lines (43 loc) · 1.12 KB
/
session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const session = require('express-session');
const sessionSecret = 'leyndarmál';
const app = express();
app.use(session({
name: 'counter.sid',
secret: sessionSecret,
resave: false,
saveUninitialized: false,
}));
app.use((req, res, next) => {
const { originalUrl } = req;
let { views } = req.session;
if (!views) {
req.session.views = {};
views = {};
}
if (!views[originalUrl]) {
views[originalUrl] = 0;
}
views[originalUrl] += 1;
next();
});
app.get('/', (req, res) => {
const url = '/';
const count = req.session.views[url] || 0;
res.send(`Þú hefur opnað ${url} ${count} sinnum`);
});
app.get('/foo', (req, res) => {
const url = '/foo';
const count = req.session.views[url] || 0;
res.send(`Þú hefur opnað ${url} ${count} sinnum`);
});
app.get('/bar', (req, res) => {
const url = '/bar';
const count = req.session.views[url] || 0;
res.send(`Þú hefur opnað ${url} ${count} sinnum`);
});
const hostname = '127.0.0.1';
const port = 3000;
app.listen(port, hostname, () => {
console.info(`Server running at http://${hostname}:${port}/`);
});