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

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

The app and config folder holds the core of the application. app consists of models, views, controllers and mailers.

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 templating system requires.

The views folder defaultly contains

  1. includes/ - which contains a. head.jade - meta information, scripts, stylesheets b. header.jade - contains the logo and the naviagation links c. foot.jade - usually javascript d. footer.jade - footer links
  2. layouts/ - usually contains the page layouts.
  3. 404 and 500

Controllers

Mailers

Config

  1. Passport
  2. Express
  3. Config environments and environment specific stuff

Using middlewares

Tests

Clone this wiki locally