我有以下代碼:req.session快遞會話不是持久的
var express = require('express');
var cookieParser = require('cookie-parser');
var http = require('http')
var app = express();
app.use(cookieParser());
var session = require('express-session');
app.use(session({
resave: false,
saveUninitialized: true,
secret: 'sdlfjljrowuroweu',
cookie: { secure: true }
}));
app.get('/test', test);
function test(req, res) {
var sess = req.session;
console.log('before', sess);
if (sess.views) {
sess.views++
req.session.save();
res.setHeader('Content-Type', 'text/html')
res.write('<p>views: ' + sess.views + '</p>')
res.write('<p>expires in: ' + (sess.cookie.maxAge/1000) + 's</p>')
res.end();
} else {
sess.views = 1;
req.session.save();
res.end('welcome to the session demo. refresh!')
}
console.log('after', sess);
return;
}
var server = http.createServer(app);
server.listen(8181);
並重新載入頁面,我只是不斷收到的觀看次數0消息。
檢查控制檯,這是輸出每次:
before { cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true,
secure: true } }
after { cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true,
secure: true },
views: 1 }
因此,它似乎並沒有在所有
您使用的是安全連接嗎?又名'https://',我懷疑你不是,但是你已經在'express.session(...)'裏面設置了'secure'標誌爲true,這意味着你將永遠無法訪問該cookie(所以會話將無法正常工作)。 –