How to load variables before page renders in node.js?

My question is how to make sure my variables get loaded before the page gets rendered ? I use node.js with express and everyauth.

This is the code:

app.get('/', function(req, res) {
   app.locals.rep = global.repos;
   res.render('index'); 
});

Currently, the page gets loaded, but the paragraph containing the variable is empty and I get an error: "Cannot read property '...' of undefined [...]". As I refresh the page, the content flows in.

index.jade

extends layout
block content
  h1= title
  - if (!everyauth.loggedIn)
    a(href="/auth/github") Login with github
  - else
    h3
      a(href='/logout') Logout
      h3 GitHub User Data
      - each r in rep
        p= r.name
        p= r.description

This is where I set the repos variable:

var repos;
global.repos = [];

everyauth.github
.appId(config.gh_clientId)
.appSecret(config.gh_secret)
.findOrCreateUser( function (sess, accessToken, accessTokenExtra, ghUser) {

if (typeof usersByGhId[ghUser.id] === 'undefined') {

  usersByGhId[ghUser.id] = addUser('github', ghUser);

  var options = { 
            host: "api.github.com", 
            path: "/users/cmarius02/repos",
            method: "GET",
            headers: {
               "User-Agent": "github-connect" 
            }
  };

  var request= https.request(options, function(response){
    var body='';
    response.on("data", function(chunk){
      body+=chunk.toString("utf8");
    });

    response.on("end", function(){
      var json=JSON.parse(body);

      // console.log(json);  

      global.repos = [];
      for (var k in json)
        if ({}.hasOwnProperty.call(json, k)) {
          global.repos.push({
            name: json[k].name,
            description: json[k].description
          });
        }
    });
  });
  request.end();
  return usersByGhId[ghUser.id];

} else {
  return usersByGhId[ghUser.id];
}   })

.redirectPath('/');

It's my first day of node.js so please be pacient. Thank you in advance.

I use passport instead of everyauth, but I think the approach should be similar

Express uses middleware to chain up code, and each middleware is responsable of calling the next one (even if they dont know which is it)

I do something like this:

    app.all ('*', function (req,res,next) {
      // Add the user from the session
      if req.user?
          res.locals.session_user = req.user
      // Add the flash messages
      res.locals.flash_messages = req.flash()

      next()
    });

    app.get('/', function(req, res) {
      res.render('index'); 
    });

The app.all means that the route will match get, put and post request. The * is a regexp that matches all routes, so the first callback will be called before all your routes

req.user is filled by passport, I imagine everyauth has something similar.

res.locals is the one that allows you to pass information to the view. Then, in the view, you can just reference the element as this.session_user (I dont use jade, but you get the point).

Also note that with the same approach, I pass flashes between requests to the view.

The order is important!. And next() assures you that your actual route wont be handled until the previous one hasnt resolved. If you need, you can call next inside another callback in that method.