Is there a common convention for breaking up and modularizing the app.js
file in an Express.js application? Or is it common to keep everything in a single file?
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)
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).
If you are a fan of CoffeeScript (I am not) and reeeeaaaaaally want the L&F of Rails, there is also Tower.js.
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.
nodejs-express-mongoose-demo is very similar to how I have mine structured. Check it out.