Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

node.js - Express 4 Sessions not persisting when restarting server

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...