Search code examples
node.jssessionredisexpress-session

How can I get the current session information from express-session?


I am using express-session to store my session data for my Node app as follows:

import express from 'express';
import session from 'express-session';
import connectRedis from 'connect-redis';

const app = express();
const RedisStore = connectRedis(session);

app.use(session({
    secret: 'keyboard kat',
    store: new RedisStore({
        url: '//redis:6379',
    }),
    resave: false,
    saveUninitialized: false,
    proxy: true,
    cookie: {
        secure: true,
    },
}));

and I'm trying to get the information of the current session in the browser. I tried typing session in the console to no avail. Similarly, adding a console.log(session) below where I set up use of the session in the app doesn't work. How can I get the current session information from express-session?


Solution

  • You can simply use your app with a function that references the session

    app.use(function (req, res, next) {
        console.log(req.session)
    })
    

    but it will print to the command line running your Node server rather than the console of the browser.