Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have taken another approach here. Not saying it is the best, but let me explain.</p> <ol> <li>Each schema (and model) is in its own file (module)</li> <li>Each group of routes for a particular REST resource are in their own file (module)</li> <li>Each route module just <code>require</code>s the Mongoose model it needs (only 1)</li> <li>The main file (application entry point) just <code>require</code>s all route modules to register them.</li> <li>The Mongo connection is in the root file and is passed as parameter to whatever needs it.</li> </ol> <p>I have two subfolders under my app root - <code>routes</code> and <code>schemas</code>.</p> <p>The benefits of this approach are: </p> <ul> <li>You only write the schema once.</li> <li>You do not pollute your main app file with route registrations for 4-5 routes per REST resource (CRUD)</li> <li>You only define the DB connection once</li> </ul> <p>Here is how a particular schema file looks: </p> <p><strong>File: /schemas/theaterSchema.js</strong></p> <pre><code>module.exports = function(db) { return db.model('Theater', TheaterSchema()); } function TheaterSchema () { var Schema = require('mongoose').Schema; return new Schema({ title: { type: String, required: true }, description: { type: String, required: true }, address: { type: String, required: true }, latitude: { type: Number, required: false }, longitude: { type: Number, required: false }, phone: { type: String, required: false } }); } </code></pre> <p>Here is how a collection of routes for a particular resource looks:</p> <p><strong>File: /routes/theaters.js</strong></p> <pre><code>module.exports = function (app, options) { var mongoose = options.mongoose; var Schema = options.mongoose.Schema; var db = options.db; var TheaterModel = require('../schemas/theaterSchema')(db); app.get('/api/theaters', function (req, res) { var qSkip = req.query.skip; var qTake = req.query.take; var qSort = req.query.sort; var qFilter = req.query.filter; return TheaterModel.find().sort(qSort).skip(qSkip).limit(qTake) .exec(function (err, theaters) { // more code }); }); app.post('/api/theaters', function (req, res) { var theater; theater.save(function (err) { // more code }); return res.send(theater); }); app.get('/api/theaters/:id', function (req, res) { return TheaterModel.findById(req.params.id, function (err, theater) { // more code }); }); app.put('/api/theaters/:id', function (req, res) { return TheaterModel.findById(req.params.id, function (err, theater) { // more code }); }); app.delete('/api/theaters/:id', function (req, res) { return TheaterModel.findById(req.params.id, function (err, theater) { return theater.remove(function (err) { // more code }); }); }); }; </code></pre> <p>And here is the root application file, which initialized the connection and registers all routes:</p> <p><strong>File: app.js</strong></p> <pre><code>var application_root = __dirname, express = require('express'), path = require('path'), mongoose = require('mongoose'), http = require('http'); var app = express(); var dbProduction = mongoose.createConnection('mongodb://here_insert_the_mongo_connection_string'); app.configure(function () { app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(application_root, "public"))); app.use('/images/tmb', express.static(path.join(application_root, "images/tmb"))); app.use('/images/plays', express.static(path.join(application_root, "images/plays"))); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.get('/api', function (req, res) { res.send('API is running'); }); var theatersApi = require('./routes/theaters')(app, { 'mongoose': mongoose, 'db': dbProduction }); // more code app.listen(4242); </code></pre> <p>Hope this was helpful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload