-
Notifications
You must be signed in to change notification settings - Fork 67
/
example-mjs.mjs
49 lines (43 loc) · 1.1 KB
/
example-mjs.mjs
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
import express from "express";
import session from "express-session";
import { ConnectSessionKnexStore } from "../dist/index.js";
// import { ConnectSessionKnexStore } from "connect-session-knex";
import knexConstructor from "knex";
const app = express(); // Express 4
const store = new ConnectSessionKnexStore({
knex: knexConstructor({
client: "sqlite",
// connection: ":memory:",
connection: {
filename: "connect-session-knex.sqlite",
},
}),
cleanupInterval: 0, // disable session cleanup
});
app.use(
session({
secret: "keyboard cat",
cookie: {
maxAge: 30000, // 30 seconds for testing
},
store,
resave: false,
saveUninitialized: false,
}),
);
app.use("/", (req, res) => {
const n = req.session.views || 0;
req.session.views = n + 1;
res.end(`${n} views`);
});
app.listen(3000);
setInterval(() => {
store.length().then((length) => {
console.log(`There are ${JSON.stringify(length)} sessions`);
});
}, 2000);
setInterval(() => {
store.clear().then((length) => {
console.log(`Cleared ${JSON.stringify(length)} sessions`);
});
}, 30000);