I have 2 routers, for the example
router1
app.post('/consultas', function (req, res) {
req.session.nombre = 'administrador';
console.log('prueba',req.session.nombre); // ahi la session se guarda bien
});
router2
router.get('/', function (req, res) {
console.log('recuperar session ', req.session);// sale undefined
}
How can I get the session on both routers?
Seeing your logic you could save the session in a middleware and in the two routes apply the middleware already created there you will obtain the saved data of the user so you can put your middleware in your route
req.session.name here all requests that use this middleware will have access to req.session.name
a little more explained here
how to create a middleware
[ https://expressjs.com/en/guide/writing-middleware.html][1]
how to use them
[ https://expressjs.com/en/guide/using-middleware.html][1]
I solved it with cookieSession
Note : It must be in that order for the routes to get the session from the server.
The use can be used like this (it is not necessary
middlewares
), saving in the session variableand in the other routes the session variable is available to use