8

I'd like to know my options for how to structure my routes in Express.js. Here's my apps directory right now:

  ├── public
  │   ├── images
  │   ├── javascripts
  │   │   ├── showLogin.js
  │   │   └── showSignup.js
  │   └── stylesheets
  │       ├── dash.styl
  │       ├── reset.styl
  │       ├── space.styl
  │       ├── style.css
  │       ├── style.styl
  │       └── typography.styl
  ├── routes
  │   └── index.js
  ├── views
  │   ├── dash.jade
  │   ├── index.jade
  │   ├── layout.jade
  │   └── space.jade
  ├── app.js
  └── package.json

Is it best to break up routes/index.js into several single route files? I've tried this, and apparently I'm not much good at it, haha.

Another option is to introduce a lib directory somewhere. Where does one usually do that?

Any other advice would rock!

Costa Michailidis
  • 7,691
  • 15
  • 72
  • 124

3 Answers3

11

A couple of us at work created a small reference app to codify a standard Express app structure. It's not overly complex or anything, we just tried to create something that was easily understandable, and had structure that would allow the app to grow over time.

You can find it here: https://github.com/EAAppFoundry/tableau

While you're there, feel free to give us feedback. I'm sure it could be improved.

Hope it helps!

Don
  • 677
  • 5
  • 10
  • Looks really interesting. I don't know if I'm experienced enough to give valuable input, but here goes: I've seen someone put their config into a JSON file publicConfig.json and privateConfig.json, keeping the private config one directory level above the app for security reasons, that seemed like an interesting way to do it. Hope that's helpful! – Costa Michailidis Aug 04 '12 at 16:31
  • 1
    Thanks! That's a good idea about the privateConfig.json. I've been meaning to do something that will hide sensitive info (primarily db connect info) b/c checking that stuff in to github isn't the best idea. :) – Don Aug 05 '12 at 20:23
  • Agreed. I haven't tried it myself yet, so if you have some success with that, do share! – Costa Michailidis Aug 07 '12 at 04:29
  • If you follow the convention of only the "/public" directory serving static files (app.use(express.static();), then putting privateConfig.json in the app dir should be fine. You really shouldn't be (and probably aren't) serving the app base dir statically. @Don Add "privateConfig.json" (and any other sensitive files) as a line in your .gitignore. You could even wildcard it "private*", then prefix any sensitive files by convention. – Spain Train Nov 01 '12 at 21:40
5

Check out my answer to ExpressJS How to structure an application?. I like to group routes into controllers (login, shopping_cart, preferences, photos, etc) and cleanly separate concerns.

Community
  • 1
  • 1
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
2

Take a look at the work that PayPal did with KrakenJS. It's built on top of express, but it adds a configuration layer to it that keeps things like routes super tidy.

Lenny Markus
  • 3,398
  • 2
  • 24
  • 30