Get res.locals values in another midleware

1

I am trying to make the page renderize the information of a user that is stored in res.locals , however the router when rendering is in another file, therefore I can not get access to the res.locals information.

// root.js

var express    =require('express');
var router_app =require('./modulos/rutas');
var web        =express();  

web.use('/dashboard',router_app);

web.post('/login',function(entra,sale){ //AQUI INICIA EL LOGIN	
	valida.logIn(user).then((user)=>{
    //Tiene que estar dentro de esta promesa puesto que tiene 
    //Guardar la informacion que retorna de esta promesa
		sale.locals = {user : "Informacion Aqui"};
    //Hasta aqui recibe bien la informacion

		sale.redirect('/dashboard');
	}).catch((error)=>{
		console.log("Rechazado"+error);
	});
});

So far we're doing fine.

//rutas.js

var express=        require('express');
var router =		express.Router();

router.get('/',(entra,sale)=>{

	console.log(sale.locals);
 //Aqui no imprime nada, la informacion ya no esta
});


module.exports = router;

Here the console does not print anything for me, simply the information that I saved before in locals , is not ... The idea that to save in locals the id of a user to be able to search the information of said user and rende-rizar the page with all the information lowered of the BD with respect to the user. I already have some time researching about res.locals and I have not yet come to the solution of this problem.

I was reading that it has to do with what .render calls .end and that's why the .locals information is removed.

    
asked by Eleazar Ortega 26.11.2017 в 22:51
source

2 answers

1

res.locals only exists during this request

  

An object that contains a local variable scoped to the   request, and therefore available only to the view (s) rendered during   that request

app.locals persists throughout the life cycle of the application

  

Eleven sets, the value of app.locals properties persist throughout the   life of the application

Therefore what you want to do should be as follows:

app.post('/login',function(req, res){  
    valida.logIn(user).then((user)=>{
        req.app.locals = {user : "Informacion Aqui"};
        res.redirect('/dashboard');
    }).catch((error)=>{
        console.log("Rechazado"+error);
    });
});

And then:

router.get('/',(req, res)=>{
    console.log(req.app.locals);
});

I have the doubt about the property name entra.app . According to me, it does not matter if you have declared the app as web , it should be accessed the same and not as entra.web . But if it does not come first, try the second.

    
answered by 30.11.2017 в 14:09
0

I recommend you use passport, or at least take a look at express-session and how to keep persistent sessions between requests

link

link

This is an example of express-session

// Use the session middleware
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))

// Access the session as req.session
app.get('/', function(req, res, next) {
  if (req.session.views) {
    req.session.views++
    res.setHeader('Content-Type', 'text/html')
    res.write('<p>views: ' + req.session.views + '</p>')
    res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>')
    res.end()
  } else {
    req.session.views = 1
    res.end('welcome to the session demo. refresh!')
  }
})
    
answered by 30.11.2017 в 21:22