重新启动服务器时,Express 4 会话不持久 [英] Express 4 Sessions not persisting when restarting server
问题描述
我有一个 Express 4 应用程序来设置会话.
I have an Express 4 app setup to have sessions.
// Sessions
app.use(cookieParser());
app.use(session({ secret: "some-secret" }));
// Signup
app.post("/signup", function (req, res) {
create_user(req.body.user, function (err, user_id) {
req.session.user_id = user_id;
res.redirect("/admin");
});
});
当我提交表单时,它会将 user_id 保存到 req.session.但是,当我重新启动服务器时,会话消失了.
When I submit the form, it saves the user_id to the req.session. However, when I restart the server, the session is gone.
为什么不坚持?我是否缺少一些配置?
Why isn't it persisting? Am I missing some configuration?
推荐答案
express-session 的默认会话存储是 MemoryStore,顾名思义,它仅将会话存储在内存中.如果您需要持久性,Express 有许多可用的会话存储.一些例子:
The default session store for express-session is MemoryStore, which as the name suggests, stores sessions in memory only. If you need persistence, there are many session stores available for Express. Some examples:
有关更新和更完整的列表,请访问兼容的会话存储一>.
For a updated and more complete list visit Compatible Session Stores.
这篇关于重新启动服务器时,Express 4 会话不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!