In this article, we will see how to expire the session after 1 min of inactivity in express-session of Express.js.
Prerequisites
Requires Modules:
npm install express
npm install express-session
Call API:
var session = require('express-session')
To expire the session after 1 min of inactivity in the express-session of Express.js we use expires: 60000 in the middleware function.
Project Structure:

The below example illustrates the above approach:
Example:
Filename: app.js
Javascript
const express = require( 'express' ),
session = require( 'express-session' ),
app = express();
app.use(
session({
secret: "I am girl" ,
resave: true ,
saveUninitialized: false ,
cookie: {
expires: 60000
}
})
);
app.get( '/session' , function (req, res, next) {
if (req.session.views) {
req.session.views++
res.write(
'
<p> Session expires after 1 min of in activity: '
+ (req.session.cookie.expires) + '</p>
' )
res.end()
} else {
req.session.views = 1
res.end( ' New session is started' )
}
})
app.listen(3000, function () {
console.log( "Express Started on Port 3000" );
});
|
Steps to run the program:
Run the index.js file using the below command:
node app.js

Now to set your session, just open the browser and type this URL :
http://localhost:3000/session
Output: After 1 min of inactivity it will start the new session, old session is expired.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
20 Mar, 2023
Like Article
Save Article