OK, it's been a while and this is a popular question, so I've gone ahead and created a scaffolding github repository with JavaScript code and a long README about how I like to structure a medium-sized express.js application.
focusaurus/express_code_structure is the repo with the latest code for this. Pull requests welcome.
Here's a snapshot of the README since stackoverflow doesn't like just-a-link answers. I'll make some updates as this is a new project that I'll continue updating, but ultimately the github repo will be the up-to-date place for this information.
Express Code Structure
This project is an example of how to organize a medium-sized express.js web application.
Current to at least express v4.14 December 2016
How big is your application?
Web applications are not all the same, and there's not, in my opinion, a single code structure that should be applied to all express.js applications.
If your application is small, you don't need such a deep directory structure as exemplified here. Just keep it simple and stick a handful of .js
files in the root of your repository and you're done. Voilà.
If your application is huge, at some point you need to break it up into distinct npm packages. In general the node.js approach seems to favor many small packages, at least for libraries, and you should build your application up by using several npm packages as that starts to make sense and justify the overhead. So as your application grows and some portion of the code becomes clearly reusable outside of your application or is a clear subsystem, move it to it's own git repository and make it into a standalone npm package.
So the focus of this project is to illustrate a workable structure for a medium-sized application.
What is your overall architecture
There are many approaches to building a web application, such as
- Server Side MVC a la Ruby on Rails
- Single Page Application style a la MongoDB/Express/Angular/Node (MEAN)
- Basic web site with some forms
- Models/Operations/Views/Events style a la MVC is dead, it's time to MOVE on
- and many others both current and historical
Each of these fits nicely into a different directory structure. For the purposes of this example, it's just scaffolding and not a fully working app, but I'm assuming the following key architecture points:
- The site has some traditional static pages/templates
- The "application" portion of the site is developed as a Single Page Application style
- The application exposes a REST/JSON style API to the browser
- The app models a simple business domain, in this case, it's a car dealership application
And what about Ruby on Rails?
It will be a theme throughout this project that many of the ideas embodied in Ruby on Rails and the "Convention over Configuration" decisions they have adopted, though widely accepted and used, are not actually very helpful and sometimes are the opposite of what this repository recommends.
My main point here is that there are underlying principles to organizing code, and based on those principles, the Ruby on Rails conventions make sense (mostly) for the Ruby on Rails community. However, just thoughtlessly aping those conventions misses the point. Once you grok the basic principles, ALL of your projects will be well-organized and clear: shell scripts, games, mobile apps, enterprise projects, even your home directory.
For the Rails community, they want to be able to have a single Rails developer switch from app to app to app and be familiar and comfortable with it each time. This makes great sense if you are 37 signals or Pivotal Labs, and has benefits. In the server-side JavaScript world, the overall ethos is just way more wild west anything goes and we don't really have a problem with that. That's how we roll. We're used to it. Even within express.js, it's a close kin of Sinatra, not Rails, and taking conventions from Rails is usually not helping anything. I'd even say Principles over Convention over Configuration.
Underlying Principles and Motivations
The app symlink trick
There are many approaches outlined and discussed at length by the community in the great gist Better local require() paths for Node.js. I may soon decide to prefer either "just deal with lots of ../../../.." or use the requireFrom modlue. However, at the moment, I've been using the symlink trick detailed below.
So one way to avoid intra-project requires with annoying relative paths like require("../../../config")
is to use the following trick:
- create a symlink under node_modules for your app
- cd node_modules && ln -nsf ../app
- add just the node_modules/app symlink itself, not the entire node_modules folder, to git
- git add -f node_modules/app
- Yes, you should still have "node_modules" in your
.gitignore
file
- No, you should not put "node_modules" into your git repository. Some people will recommend you do this. They are incorrect.
- Now you can require intra-project modules using this prefix
var config = require("app/config");
var DealModel = require("app/deals/deal-model")
;
- Basically, this makes intra-project requires work very similarly to requires for external npm modules.
- Sorry, Windows users, you need to stick with parent directory relative paths.
Configuration
Generally code modules and classes to expect only a basic JavaScript options
object passed in. Only app/server.js
should load the app/config.js
module. From there it can synthesize small options
objects to configure subsystems as needed, but coupling every subsystem to a big global config module full of extra information is bad coupling.
Try to centralize creation of DB connections and pass those into subsystems as opposed to passing connection parameters and having subsystems make outgoing connections themselves.
NODE_ENV
This is another enticing but terrible idea carried over from Rails. There should be exactly 1 place in your app, app/config.js
that looks at the NODE_ENV
environment variable. Everything else should take an explicit option as a class constructor argument or module configuration parameter.
If the email module has an option as to how to deliver emails (SMTP, log to stdout, put in queue etc), it should take an option like {deliver: 'stdout'}
but it should absolutely not check NODE_ENV
.
Tests
I now keep my test files in the same directory as their corresponding code and use filename extension naming conventions to distinguish tests from production code.
foo.js
has the module "foo"'s code
foo.tape.js
has the node-based tests for foo and lives in the same dir
foo.btape.js
can be used for tests that need to execute in a browser environment
I use filesystem globs and the find . -name '*.tape.js'
command to get access to all my tests as necessary.
How to organize code within each .js
module file
This project's scope is mostly about where files and directories go, and I don't want to add much other scope, but I'll just mention that I organize my code into 3 distinct sections.
- Opening block of CommonJS require calls to state dependencies
- Main code block of pure-JavaScript. No CommonJS pollution in here. Don't reference exports, module, or require.
- Closing block of CommonJS to set up exports