Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Node.js file structure with express

consider making a node-guides site like rails guides?

Approach 1:

TJ HollowayChuck (creator of Express)

"personally I find large rails-style structure difficult to work with for large applications (great for small ones however). A more modular approach like Drupal is a big win as far as management goes but it can complicate some things (layout templates) so you kind of need compromise sometimes.

We're going more towards being a bit less DRY but high lots of modularity, duplicating the odd template here and there isn't a show-stopper, but in general I think the typical rails style ./{views,models,controllers} is a bad way to structure big apps, I prefer something like:

./modules/{student,teacher,admin}/{views,routes} "

So essentially he is suggesting a student module (instead of model) and then tying that to views / routes... Interesting... We shall see...

Approach 2: Something in between rails and current

Maybe all the controller stuff goes in routes/index.js. Or all the controller stuff goes in one file?

I like having the model stuff separate.

I also like having the routes/config stuff separate from the app.js.

Essentially use approach #3 but keep the routes/controller flat.

Goal:Keep routes / controllers combined in one file.

Goal: Keep controllers in that combined SMALL.

To do that have FAT models, and helper functions (in /helpers/) as needed.
Skinny controller.

Approach 3: Like Rails MVC

//modeled after the Rails file strucuture

Like this:

TODO: Make this markdown

FAT MODELS, SKINNY Controllers, Views that have (almost) no business logic in them.

Config files




views/shared (if they are meant to be shared across views. it not, just in the same folder as the others.)

partial names should also start with an '_'

MVC Example

For a given profile:

DB table / collection

profiles (plural)

model (this file contains the mongoose schema). This should contain the bulk of the business logic. Bulk of data manipulation / db interaction.

models/profile.js (singular)

controller - This is the glue that binds the model & view together. With express the controller is in app.js. I like abstracting that out more.

controllers/profiles_controller.js (plural)

possible future additions

/helper/ /config/ /db/

OR put config.js db.js in the same folder as app.js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.