[英]Node.js Express disable automatic session creation
If I enable the session feature of express via app.use(express.session({secret: "12345"}));
如果我通过
app.use(express.session({secret: "12345"}));
启用express的会话功能app.use(express.session({secret: "12345"}));
the session cookie is set when the user first hits a page. 会话cookie在用户首次点击页面时设置。
How can I disable this behavior and decide manually when to create a cookie, for example after a successful login? 如何禁用此行为并手动决定何时创建cookie,例如在成功登录后? I am aware that I could just construct a cookie-header manually, but I would like to stay with express.session.
我知道我可以手动构建一个cookie-header,但我想继续使用express.session。
Define the session support as middleware, but don't use use
: 将会话支持定义为中间件,但不要使用
use
:
var sessions = express.session({
// etc
});
...
app.get('/', function (req, resp) {
// No session
});
app.post('/user', sessions, function (req, resp) {
// Has sessions
Imagine you have a login method... I SUPPOSE you could do like that. 想象一下,你有一个登录方法......我想你可以做这样的。
var sessionMW = express.session({secret:"12345"});
function login(req, res, next){
//...
if(success){
return expressMW(req, res, next);
}
}
I'm not sure if this option existed when this question was originally was posted but I was able to set the saveUninitialized
option as false
to do this. 我不确定此问题最初是否已发布时是否存在此选项但我能够将
saveUninitialized
选项设置为false
来执行此操作。
https://github.com/expressjs/session#saveuninitialized https://github.com/expressjs/session#saveuninitialized
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.