Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session-file-store Delete expired session files

I am using session-file-store to maintain the sessions in my Node-Express app.

session-file-store generates a new file for every session. This will create lots of files on the server over time.

Is there any option / way to automatically delete the file after the session expiry?

Here is the part of the code I am using for this -

.
.
const session = require('express-session');
const FileStore = require('session-file-store')(session);
.
.
app.use(session({
    genid: (req) => {
        return uuid() // use UUIDs for session IDs
    },
    store: new FileStore(),
    secret: MY_SECRET,
    resave: false,
    saveUninitialized: true,
    cookie: { maxAge : SESSION_COOKIE_TIMEOUT }
}));
.
.

Here is the sample file that gets generated by session-file-store -

{"cookie":{"originalMaxAge":59999,"expires":"2019-03-27T03:28:21.597Z","httpOnly":true,"path":"/"},"__lastAccess":1553657241598}
like image 507
Nis Avatar asked Nov 06 '22 19:11

Nis


1 Answers

reapInterval will purge all expired cookies from your server, e.g. to set purging to every 10 secs:

var fileStoreOptions = {
  path: "./sessions",
  reapInterval: 10,
};```
like image 91
Muppet Avatar answered Nov 15 '22 15:11

Muppet