Express会话无法保存数据

时间:2019-06-06 19:30:52

标签: node.js express

我尝试使用express-session存储一些数据。这是我的整个设置(与here相同):

var express = require('express');
var session = require('express-session')
const bodyParser = require('body-parser');
var app = express();
var engines = require('consolidate');
//app.engine('html', engines.mustache);
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.static(__dirname + '/public'));


app.set('trust proxy', 1) // trust first proxy
app.use(session({
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: true,
    cookie: { secure: true }
}))

app.use(express.urlencoded({
    extended: true
}));

这里有相关的中间件:

app.post('/dolog', function (req, res) {
    if (!req.body.nick || !req.body.password) {
        res.redirect('failedLogin')
    } else {
        let isValid: Boolean = validLogin(req.body.nick, req.body.password);
        console.log('isValid:', isValid)
        if (!isValid) {
            res.redirect('failedLogin')
        } else {
            req.session.token = newToken(req.body.nick)
            console.log('session:', req.session)
            res.redirect('account')
        }
    }
})

app.get('/account', function (req, res) {
    console.log(req.session)
    ...
});

在帖子中,我设置了令牌值,重定向到帐户后,我想读回它,但它不存在...

当我在POST中记录会话时,我得到:

session: Session {
    cookie:
    {
        path: '/',
        _expires: null,
        originalMaxAge: null,
        httpOnly: true,
        secure: true
    },
    token: 'eeUPRdzHAiIZRIqqAa5uu2Th19EQOM7i5fnKNDJZ'
}

在GET中:

Session {
    cookie:
    {
        path: '/',
        _expires: null,
        originalMaxAge: null,
        httpOnly: true,
        secure: true
    }
}

有人可以解释为什么吗?

0 个答案:

没有答案