Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8902009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:30:18+00:00 2026-06-15T01:30:18+00:00

I have set up my express/connect app to use sessions with redis as the

  • 0

I have set up my express/connect app to use sessions with redis as the store:

app.use(express.session({
        secret: "secret key here",
        store: app.redisStore,
        cookie: { maxAge: 600000 }
    }));

I have created my own flash message system by doing the following:

module.exports = function (app) {
    'use strict';

    //set-up session messages
    app.use(function (req, res, next) {

        /* set up flash*/
        if (req.session.flash === "undefined") {
            req.session.flash = [];
        }

        res.locals.messages = function () {
            var messages = req.session.flash;
            //clear out messages
            req.session.flash = [];
            return messages;
        };
        ...

Then essentially I just push flash message objects into the flash array whenever I need them. Every time the message function is used they get cleared out. This seems to work for the most part; however, while logging out I use the regenerate function and flash becomes undefined:

function logout(req, res) {
            var currentUser = req.session.currentUser;
            req.session.regenerate(function (err) {
                req.session.flash.push({"type": "info", "message": "You have been logged out."});
                console.log(currentUser + " Logged Out");
                res.redirect("/login");
                return;
            });
        }

Which seems to make sense. Regenerate obliterates the session, and since it happens after the initial request flash becomes undefined. To avoid any future problems like this I am wondering if there is there some sort of initialize function for sessions that I can override or hook into? I’d use this to set some default session values every time a session is started or regenerated.

Side-question: Is flash actually getting saved in redis?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:30:19+00:00Added an answer on June 15, 2026 at 1:30 am

    To avoid any future problems like this I am wondering if there is
    there some sort of initialize function for sessions that I can
    override or hook into? I’d use this to set some default session values
    every time a session is started or regenerated.

    No, the way you are doing this is correct. If you want to attach multiple actions to regenerate method, then you can also try something like this:

    var EventEmitter = require('events').EventEmitter;
    var session_events = new EventEmitter( );
    
    module.exports = function (app) {
        'use strict';
    
        //set-up session messages
        app.use(function (req, res, next) {
            var regenerate = req.session.regenerate;
            req.session.regenerate = function() {
                session_events.emit( "session_regenerated", req );
                regenerate.apply( req.session, arguments );
            };
    
            /* set up flash*/
            if (req.session.flash === "undefined") {
                session_events.emit( "session_initializing", req );
            }
            // other code goes here
        });
    }
    

    Then you can simply do:

    session_events.on( "session_regenerated", function(req) {
        req.session.flash.push({"type": "info", "message": "You have been logged out."});
    });
    session_events.on( "session_initializing", function(req) {
        req.session.flash = [];
    });
    

    This is a monkey patch, though ( at least the .regenerate overriding ), so I advice writing your own session store, which is not difficult at all, since session is nothing else then an entry in Redis/any other storage.

    Side-question: Is flash actually getting saved in redis?

    Yes, everything attached to session goes to session store ( Redis in your case ).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an express app that I want to use the connect-form module on
I'm using Express with connect-redis session store And I tied it with Socket.IO through
I have an Express.js app that uses connect-asssets to serve JS and CSS: app.configure(function()
I have set up an app that is registered for remote notifications. - (void)application:(UIApplication*)application
I am using sql server express 2008 with mmse. i have set up a
I'm making a web-app that makes use of nodejs, mongodb, socket.io, express and mongoose.
With Express / Connect I can set up an middleware function in either of
I'm using express.session and connect-mongo be my DataStore. What I'm facing is... in my
I have SSL set up on Node.js (and Express.js) and it seems to be
I am trying to use connect-assetmanager with express. Apparently I don't understand how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.