title: What is the proper way to access DB model's save method from within route handler function?
body: <p>I have noticed when one creates a project using Express.js, the route handler functions are now separated from the main 'app.js' file.  They are now placed in './routes/index.js'. Given this situation, how does one now save a record to the database (in this case using MongoDB accessed via Mongoose).</p>
  
  <p>Traditionally one would have had the route handler function in 'app.js', for example:</p>
  
  <pre class="lang-js prettyprint-override"><code>//Create document 
  app.post('/documents.:format?', function(req, res) {
    var d = new Document(req.body);
    d.user_id = req.currentUser.id;
    d.save(function() {
      switch (req.params.format) {
        case 'json':
          var data = d.toObject();
          // TODO: Backbone requires 'id', but can I alias it?
          data.id = data._id;
          res.send(data);
        break;
  
        default:
          req.flash('info', 'Document created');
          res.redirect('/documents');
      }
    });
  });
  </code></pre>
  
  <p>Under the new "layout" such call would be changed to something like the following (in 'app.js' file):</p>
  
  <pre class="lang-js prettyprint-override"><code>app.post('/documents.:format?', routes.add_documents);
  </code></pre>
  
  <p>With all the actual processing occurring in the file './route/index.js':</p>
  
  <pre class="lang-js prettyprint-override"><code>exports.add_documents = function(req, res){
    // Processing goes here!
  };
  </code></pre>
  
  <p>My question is how can one incorporate the original route handler function into this new add_documents function above?  In particular, how can one access the database schema model (e.g. create new instance of 'Document') so that I can access the database save method? </p>
  
  <p>Full contents of 'app.js' is follows:</p>
  
  <pre class="lang-js prettyprint-override"><code>var express = require('express')
    , routes = require('./routes')
    , mongoose = require('mongoose')
    , models = require('./models')
    , Document;
  
  var app = module.exports = express.createServer();
  
  // Configuration
  
  app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
  });
  
  app.configure('development', function(){
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    app.set('db-uri', 'mongodb://localhost/namecards');
  });
  
  app.configure('production', function(){
    app.use(express.errorHandler());
  });
  
  models.defineModels(mongoose, function() {
    app.Document = Document = mongoose.model('Document');
    db = mongoose.connect(app.set('db-uri'));
  })
  
  // Routes
  
  app.get('/', routes.index);
  
  app.get('/add', routes.add_form);
  
  app.post('/add', routes.add_document);
  
  app.listen(3000);
  console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
  </code></pre>
  
  <p>Content of 'models.js'</p>
  
  <pre class="lang-js prettyprint-override"><code>function defineModels(mongoose, fn) {
    var Schema = mongoose.Schema,
        ObjectId = Schema.ObjectId;
  
    /**
     * Model: Document
     */
    Document = new Schema({
      'surname': String,
      'given_name': String,
      'org': String,
      'phone': String
    });
  
    mongoose.model('Document', Document);
  
    fn();
  }
  
  exports.defineModels = defineModels; 
  </code></pre>
  
  <p>Content of './routes/index.js'</p>
  
  <pre class="lang-js prettyprint-override"><code>/*
   * GET home page.
   */
  
  exports.index = function(req, res){
    res.render('index', { title: 'Documents' });
  };
  
  /*
   * GET add contact page.
   */
  exports.add_form = function(req, res){
    res.render('add', { title: 'Add Document' });
  };
  
  /*
   * POST add contact page.
   */
  exports.add_document = function(req, res){
    // Save data to DB using mongoose.
  };
  </code></pre>