Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamischarles/3131872 to your computer and use it in GitHub Desktop.
Save jamischarles/3131872 to your computer and use it in GitHub Desktop.
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
http://guides.rubyonrails.org/getting_started.html#the-mvc-architecture
http://itsignals.cascadia.com.au/?p=7/

Like this: http://stackoverflow.com/questions/5778245/expressjs-how-to-structure-an-application

TODO: Make this markdown

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

Config files

/config/

/config/db.js
/config/environment.js
/config/routes.js

partials

views/shared (if they are meant to be shared across views. it not, just in the same folder as the others.)
http://stackoverflow.com/questions/6550103/a-folder-where-to-put-global-shared-partial-templates http://killswitchcollective.com/articles/13_best_practices_for_rails_partials

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