Mongoose and router files
<p>My folder structure is the following: </p>

<pre><code>bin/
  www
models/
  myMongooseModel.js
public/
  ...
routes/
  index.js
  anotherroute.js
views/
  ...
app.js
package.json
</code></pre>

<p>In the file <code>app.js</code>, I have some settings (<code>app.set</code> and <code>app.use</code> commands). In the <code>routes/</code> directory, there are files for the routes, one file for each. In those files, I handle the requests from the users. The file <code>bin/www.js</code> sets up the server. </p>

<p>The question is the following: How do I integrate Mongoose in this setup, i.e., where should I initialize it and what should I include in the router files? </p>

<p>Is this the best way?</p>

<p><code>db.js</code>:</p>

<pre><code>var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('connected');
});
</code></pre>

<p><code>index.js</code>: </p>

<pre><code>var mongoose = require('mongoose');
var MyModel = mongoose.model('myMongooseModel');
// do something with MyModel
</code></pre>

<p>How do I avoid that requests are made before the db connection is set up?</p>