-
Notifications
You must be signed in to change notification settings - Fork 67
/
example-postgres.mjs
54 lines (46 loc) · 1.12 KB
/
example-postgres.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
50
51
52
53
54
import express from "express";
import session from "express-session";
import { ConnectSessionKnexStore } from "../lib/index.mjs";
// import { ConnectSessionKnexStore } from "connect-session-knex";
import knexConstructor from "knex";
const app = express();
const knex = knexConstructor({
client: "pg",
connection: {
host: "127.0.0.1",
user: "postgres",
password: "",
database: "travis_ci_test",
},
});
const store = new ConnectSessionKnexStore({
knex,
tablename: "sessions", // optional. Defaults to 'sessions'
});
app.use(
session({
secret: "keyboard cat",
cookie: {
maxAge: 10000, // ten 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);