title: How to load variables before page renders in node.js?
body: <p>My question is how to make sure my variables get loaded before the page gets rendered ? I use node.js with express and everyauth.</p>
  
  <p>This is the code:</p>
  
  <pre><code>app.get('/', function(req, res) {
     app.locals.rep = global.repos;
     res.render('index'); 
  });
  </code></pre>
  
  <p>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.</p>
  
  <p>index.jade</p>
  
  <pre><code>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
  </code></pre>
  
  <p>This is where I set the repos variable:</p>
  
  <blockquote>
    <p>var repos;<br>
    global.repos = [];</p>
    
    <p>everyauth.github <br>
      .appId(config.gh_clientId) <br>
      .appSecret(config.gh_secret) <br>
      .findOrCreateUser( function (sess, accessToken, accessTokenExtra, ghUser) {</p>
  
  <pre><code>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];
  }   })
  </code></pre>
    
    <p>.redirectPath('/');</p>
  </blockquote>
  
  <p>It's my first day of node.js so please be pacient. Thank you in advance.</p>