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:
app.use(
session({
// ...
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days (in milliseconds)
}
// ...
})
);
Practical example
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.
const express = require('express'); // npm install express
const session = require('express-session'); // npm install express-session
const app = express();
app.use(
session({
secret: 'session-secret-here',
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days (in milliseconds)
},
})
);
// ...
app.listen(8080, () => console.log('Server started on port 8080.'));