I'm trying to fill a variable with a middleware that receives information through a request json and I'm storing it in a global variable which I later use in a render with pug, but the first loads return an empty variable, How can I ensure that it is loaded prior to viewing? here is my code
function fillVars(req, res, next){
var urlCandidates = "https://web.com/count_by?group=true";
request({url: urlCandidates,json: true }, function (error, response, body) {
if (!error && response.statusCode === 200) {
jsonTwiteros = body;
}
});
next();
}
// function to call render
function render(req, res, next){
console.log("Ingreso a render");
res.render('main',{total:jsonTwiteros } );
next();
}
function close(req, res, next){
res.end();
}
//set route
app.get('/',fillVars, render,close);