我使用express-session来处理sesseion,但很奇怪,在任何请求中设置的session属性,在get请求中都能获得,但是在post请求中都获得不了,请问是为什么啊? index.hbs <h1>{{title}}</h1> <p>Welcome to {{title}}</p> <form action="/a" method=“post”> <input type=“submit” value=“提交”> </form> app.js(部分) var session = require(‘express-session’); app.use(session({ secret: foo’, key: ‘bar’, cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days resave: false, saveUninitialized: true })); index.js var express = require(‘express’); var router = express.Router();
/* GET home page. */
router.get('/', function (req, res, next) {
console.log('come into get');
req.session.a = 'dd';
console.log(req.session);
res.render('index', {title: 'Express'});
});
router.post('/a', function (req, res, next) {
console.log('come into post');
console.log(req.session);
req.session.user = {username: 'hukaihe'};
res.redirect('/');
});
module.exports = router;
post里的session始终输出: Session { cookie: { path: ‘/’, _expires: Sun Mar 05 2017 15:35:23 GMT+0800 (中国标准时间), originalMaxAge: 2592000000, httpOnly: true } }