title: Losing global variables when require is used
body: <p>I am trying to globally define some things (underscore library, mongoose, db connection, eventually a permissions object)</p>
  
  <p>app.js </p>
  
  <pre><code>/* module dependencies */
  const express = require('express');
  const auth = require('http-auth');
  const underscore = require('underscore');
  const expressValidator = require('express-validator');
  const mongo = require('mongodb');
  const db = require('./library/db');
  /*end module dependencies */
  
  
  /*routes files*/
  const post = require('./routes/post');
  /* end route files */
  
  
  /* start the app instance */
  var app = express();
  
  
  /* authentication */
  var basic = auth({
    authRealm : "Private area.",
    authList : ['Shi:many222', 'Lota:123456']
  });
  /* end authentication*/
  
  
  /* pass app as an argument to the use method */
  app.configure(function () {
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(expressValidator);
    app.use(db);
    //app.use(underscore);
    /* config debugging */
    app.configure('development', function(){
      app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    });
    /* production instance */
    app.configure('production', function(){
      app.use(express.errorHandler());
    });
    /* end config debugging */
  });
  
  
  /* start route management */
  app.get('/post', function(req, res) {
    basic.apply(req, res, function(username) {
      post.use(app);
      post.findAll(req, res);
    });
  });
  app.get('/post/:id', function(req, res) {
    post.findById(req, res);
  });
  
  
  /* port management */
  var port = 8000;
  app.listen(port);
  console.log('Listening: port: '+port);
  </code></pre>
  
  <p>post.js</p>
  
  <pre><code>    // Module dependencies
  const underscore = require("underscore");
  
  
  // placeholder for app instance
  var app;
  
  
  // add a use function to exports that we can access from our app
  exports.use = function (appInstance) {
    // make app instance available in post.js
    app = appInstance;
  };
  
  
  /* find all rows */
  exports.findAll = function(req, res) {
    console.log('Post:findAll');
    app.db.collection('Post', function(err, collection) {
      collection.find({},{limit: 100}).toArray(function(err, items) {
        res.send(exports.parseModels(items));
      });
    });
  };
  
  
  /* parse a single or all items passed to it for formatting */
  exports.parseModels = function (items) {
    underscore.each(items, function(key, val) {
      console.log('underscore parsed an object');
    });
    return items;
  };
  </code></pre>
  
  <p>db.js</p>
  
  <pre><code>var mongo = require('mongodb');
  
  var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;
  
  var server = new Server('localhost', 27017, {auto_reconnect: true});
  var db = new Db('website', server);
  
  db.open(function(err, db) {
    if(!err) {
      //do an error here
    }
  });
  
  exports.db = db;
  </code></pre>
  
  <p>I am having problem getting the DB in the correct scope, currently when a query is called inside post.js, I get this error:</p>
  
  <pre><code>    ost:findAll
  TypeError: Cannot call method 'collection' of undefined
      at Object.exports.findAll (/Github/-api-v3/routes/post.js:19:10)
      at /Github/-api-v3/app.js:53:10
      at Basic.apply (/Github/-api-v3/node_modules/http-auth/lib/auth/basic.js:48:4)
      at /Github/-api-v3/app.js:51:9
      at callbacks (/Github/-api-v3/node_modules/express/lib/router/index.js:161:37)
      at param (/Github/-api-v3/node_modules/express/lib/router/index.js:135:11)
      at pass (/Github/-api-v3/node_modules/express/lib/router/index.js:142:5)
      at Router._dispatch (/Github/-api-v3/node_modules/express/lib/router/index.js:170:5)
      at Object.router (/Github/-api-v3/node_modules/express/lib/router/index.js:33:10)
      at next (/Github/-api-v3/node_modules/express/node_modules/connect/lib/proto.js:199:15)
  </code></pre>