search

Home  >  Q&A  >  body text

node.js - 【新手求教】express-session 保存的问题

app.use(session({
    resave: true,
    saveUninitialized: true,
    secret: 'yunBaby',
    cookie: {
        maxAge: 1000 * 60 * 30
    }
}));
......
router.get('/', function (req, res, next) {
    console.log(req.sessionID);
    console.log(req.session.name);
    if (req.session.name != null) {
        res.render('index', {title: req.session.name});
    }else{
       res.render('index', {title: '测试'});
    }
}).post('/', function (req, res) {
    ........
    if (user != null && user != "" && !pswd != null && pswd != "") {
        res.render('login', {title: "登录成功!"});
        req.session.name = user;
        console.log(req.sessionID);
        console.log(req.session.name);
    } else {
        res.render('login', {title: "用户名和密码不能为空!"});
    }
});

为啥get那里的session就是没有name,提交post之后session是成功设置了name,但是每次刷新时,get到的session的name都是 undefined . 而且输出的sessionID是一样的。

黄舟黄舟2786 days ago399

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 16:09:40

    req.session.save();

    reply
    0
  • Cancelreply