Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

【新手求教】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是一样的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

req.session.save();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...