Skip to content
Madhusudhan Srinivasa edited this page Jun 22, 2013 · 19 revisions

This wiki describes the architecture of the node-express-mongoose boilerplate.

server.js

Models

Simply drop a mongoose model file in the app/models/ directory and make sure the schema is defined

var mongoose = require('mongoose')
var Schema = mongoose.Schema

var UserSchema = new Schema({
  name: { type: String, default: '' },
  email: { type: String, default: '' },
  hashed_password: { type: String, default: '' },
  salt: { type: String, default: '' }
})

and is exposed using

// after schema declaration
mongoose.model('User', UserSchema)

As you can see in the user model file, we are using mongoose-user plugin - which does the validations and provides some generic statics like list and load.

Further down, you can use these list and load methods to write your own statics. For example

somewhere in your admin or controller

User.deleted(function (err, users) {
  // do your stuff
  // `users` is an array of deleted users
})

in app/models/user.js

UserSchema.static({

  /**
   * Lists all the deleted users
   */

  deleted: function (cb) {
    this.list({
      criteria: { deleted: true },
      sort: { name: 'desc' }
      limit: 20,
      populate: [{
        path: 'company', select: 'name', match: { deleted: false }
      }],
      select: 'name email'
    }, cb)
  }
})

Views

The views are located in app/views/ directory. The boilerplate uses jade as the templating system. If you want to use ejs (also use ejs-locals) or any other templating system, rename the .jade files to .ejs or whatever extension the templating system provides.

The views folder contains

  1. includes/ - contains
  • head.jade - meta information, scripts, stylesheets
  • header.jade - contains the logo and the naviagation links
  • foot.jade - usually javascript
  • footer.jade - footer links
  1. layouts/ - contains the layout files.
  2. 404 and 500

We are using the view-helpers middleware, which provides some generic methods. If any request comes from a mobile device, then the app automatically tries to look for a .mobile.jade file and renders it.

For example

res.render('home/index', {
  title: 'Welcome to our app'
})

If the request is coming from a mobile device then, the the app tries to look for a index.mobile.jade, if its there, it tries to render that, otherwise the index.jade is rendered.

Controllers

Mailers

Config

  1. express
  2. passport
  3. routes
  4. config environments and environment specific stuff

Using middlewares

Tests

Clone this wiki locally