I want to share the session that I create on the server to a router
on the server: app.js
var routesEvaluacion = require('./routes/evaluacion');
app.use('/evaluacion', routesEvaluacion);
...
app.use(session({
secret: 'administrador',
resave: false,
saveUninitialized: true
}))
app.post('/consultas', function (req, res) {
if (req.body.nombre === "student" && req.body.pass === "student") {
req.session.nombre = 'student';
res.redirect('/listaEvaluaciones');
}
});
and I need the session in the router evaluation.js
router.get('/autenticacion/:id',function (req, res) {
// necesito la session **req.session.nombre**
});
Once you mount a router in an Express application, any middleware declarations that occur later in the app will not be called on requests that target that router.
For example, if you do this:
So
session
it won't be available inrouter
. But it will be if you change the order:You must mount the router (in your case
evaluacion
) after defining the session and not before to fix the problem: