title: Express.js 4: How to access app.locals.<myvar> in routes/index.js?
body: <p>I want access my own variable, <code>app.locals.port</code>, from app.js, inside my routes/index.js file.</p>
  
  <p><strong>app.js:</strong></p>
  
  <pre><code>app.locals.port = 3001;
  var index = require('./routes/index');
  app.use('*', index); // use router in ./routers/index.js
  </code></pre>
  
  <p><strong>routes/index.js:</strong></p>
  
  <pre><code>var app = require('../app');
  
  console.log('app.locals.port: ' + app.locals.port);
  </code></pre>
  
  <p>Output in my log when running <code>npm start</code> --> <code>nodemon -e css,ejs,js,json,html,pug ./bin/www</code>:</p>
  
  <pre><code>[nodemon] 1.11.0
  [nodemon] to restart at any time, enter `rs`
  [nodemon] watching: *.*
  [nodemon] starting `node ./bin/www`
  app.locals.port: undefined
  </code></pre>
  
  <p>My current workaround is to use a global:</p>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>global.port = 3001;
  </code></pre>
  
  <p><strong>routes/index.js</strong></p>
  
  <pre><code>console.log('global.port: ' + global.port);
  </code></pre>
  
  <p>Thank you.</p>