req.session is undefined using express-session

前端 未结 4 1973
梦毁少年i
梦毁少年i 2020-12-30 04:09

I am just starting to learn how to use cookies with node and express and I would like some help with getting this to work. I tried to follow the expressjs/session tutorial o

4条回答
  •  醉话见心
    2020-12-30 05:03

    To get session data

    first , You have to initialize express-session with

    app.use(session({ resave: true ,secret: '123456' , saveUninitialized: true}));
    

    then When you want to put something in the session

    req.session.firstName = 'Aniruddha';
    req.session.lastName = 'Chakraborty';
    

    Then you can check without errors

    console.log('req.session: '+req.session.firstName);
    

    Note: This is express-sessions work!

提交回复
热议问题