EN
Express.js - set session expiration time
3 points
In this article, we would like to show you how to set session expiration time in Express.js.
Quick solution:
xxxxxxxxxx
1
app.use(
2
session({
3
// ...
4
cookie: {
5
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days (in milliseconds)
6
}
7
// ...
8
})
9
);
In this section, we present a practical of how to use cookie
maxAge
to set session expiration time to 7 days in a simple Express.js application.
xxxxxxxxxx
1
const express = require('express'); // npm install express
2
const session = require('express-session'); // npm install express-session
3
4
const app = express();
5
6
app.use(
7
session({
8
secret: 'session-secret-here',
9
resave: false,
10
saveUninitialized: true,
11
cookie: {
12
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days (in milliseconds)
13
},
14
})
15
);
16
17
// ...
18
19
app.listen(8080, () => console.log('Server started on port 8080.'));