G
G
Galdar Turin2021-12-02 13:48:15
Node.js
Galdar Turin, 2021-12-02 13:48:15

How does a session work in express?

I read about the express-session session, did it according to the example, but why from the site if you make a request every time a new session, but from the postman it is the same?

session
// Настраиваем сессию
app.use(session({
    secret: manifest.session.secret, // Используется для подписи файлов cookie, связанных с идентификатором сеанса
    store: new RedisStore({client: redis.createClient(manifest.redis.port, manifest.redis.ip)}), // (использовать сессию хранения Redis)
    saveUninitialized: true, // Следует ли автоматически сохранять неинициализированные сеансы, рекомендуется false
    resave: true, // Следует ли повторно сохранять сессию каждый раз, рекомендуется false
    cookie: {
        maxAge: 86400000 // 24 часа в мс 
    }
}));
  
// Проверяем, нормальная ли сессия
app.use( function (req, res, next) {

    if (!req.session) 
    {
        return next( new Error( 'Error session' ) )
    }
   
    console.log("\n===СЕССИЯ\n", req.sessionID, "\n===\n");

    next(); // Следующее промежуточное ПО

});


I suspect that OPTIONS requests are coming from the site, and then POST from the fetch function, but is it really because of this different session? Yes, the question is, what does the session change depend on, why does it change through the fetch function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur Aralin, 2021-12-04
@ArturAralin

express-sessionkeeps track of the session via a cookie. Perhaps you are not sending cookies that you previously received from the server and therefore get a new session each time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question