node.jssessionexpress-sessionsession-store

express session-file-store session count not view-count


I am using express-session for session management in my node js application. using session-file-store. I want to count the number of sessions, to restrict new sessions after maxAllowed sessions.

Is there any method supported for doing that? I dont want view count, I just want session count.


Solution

  • To count session using session-file-store can be done as shown below..

    //file store parameters
    var sess_options = {
    path: "/tmp/sessions/",
    useAsync: true
    };
    // sess_count variable
    var sess_count = 0;
    
    var file_store = new FileStore(sess_options);
    router.use(session({
    store : file_store,
    resave : false,
    saveUninitialized : true,
    secret : 'asts123',
    rolling : false,
    cookie : {  httpOnly: false,
    secure : true,
    maxAge : 60000
    }
    }));
    
    router.use(function(req, res, next) {
    file_store.length(function session_count(a,len) {
    sess_count = len;
    console.log("Total active session count is " + sess_count);
    console.log("============");
    });
    return next();
    });