How to structure a express.js application?

I have mine broken up as follows:

~/app
|~controllers
| |-monkey.js
| |-zoo.js
|~models
| |-monkey.js
| |-zoo.js
|~views
| |~zoos
|   |-new.jade
|   |-_form.jade
|~test
|  |~controllers
|    |-zoo.js
|  |~models
|    |-zoo.js
|-index.js

I use Exports to return what’s relevant. For instance, in the models I do:

module.exports = mongoose.model('PhoneNumber', PhoneNumberSchema);

and then if I need to create a phone number, it’s as simple as:

var PhoneNumber = require('../models/phoneNumber');
var phoneNumber = new PhoneNumber();

if I need to use the schema, then PhoneNumber.schema

(which assumes that we are working from the routes folder and need to go 1 level up and then down to models)


EDIT 4

The express wiki has a list of frameworks built on top of it.

Of those, I think Twitter’s matador is structured pretty well. We actually used a very similar approach to how they load up parts of the app.

derby.js also looks extremely interesting. It’s akin to meteor without all of the hype and actually gives credit where credit is due (notably, node and express).


EDIT 3

If you are a fan of CoffeeScript (I am not) and reeeeaaaaaally want the L&F of Rails, there is also Tower.js.


EDIT 2

If you are familiar with Rails and don’t mind the bleed-over of some concepts there is Locomotive. It is a light-weight framework built on Express. It has a very similar structure as RoR and carries over some of the more rudimentary concepts (such as routing).

It’s worth checking out even if you don’t plan to use it.


EDIT 1

nodejs-express-mongoose-demo is very similar to how I have mine structured. Check it out.

Leave a Comment