Express 4 Sessions not persisting when restarting server

末鹿安然 提交于 2019-11-26 20:23:40

问题


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");
    });
});

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?


回答1:


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:

  • Cookie store
  • Redis store
  • MongoDB store
  • CouchDB store
  • Riak store
  • memcached store
  • leveldb store
  • MySQL store
  • PostgreSQL store
  • Firebase store


来源:https://stackoverflow.com/questions/23260676/express-4-sessions-not-persisting-when-restarting-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!