What's the best recommended format or directory structure for an Express project in Node.js?

Date: Sat Apr 26 2014 Express »»»» Best Practice
The Express framework for Node.js offers great support for developing applications.  It has a good routing system, support for multiple templating engines, and an interesting system called "middleware" for plugging support modules into certain URL's of your website.  Say you want certain paths to require user credentials, and other paths to be open for anybody to use?  You simply insert user authentication middleware into the desired routes.

One thing the Express framework does not do is specify a layout for your application.   Instead we have complete flexibility to either organize it ourselves, or shoot ourselves in the foot.

For my book Node Web Development (see links in sidebar) I thought about this quite a bit, and came to this sort of organization:

Certain files that weave the whole application together live at the top level directory.  This particular application can use the cluster module to load-share over multiple CPU cores, so in addition to the app.js that contains all the Express glue is cluster.js and workforce.js for the clustering support.

The child directories are split into Model (models-xyzzy), View (views), and Controller (routes) roles to attempt to follow the MVC pattern.  This may or may not be a good idea, but I thought for the book it would be good to demonstrate one way of implementing that pattern.  The "public" directory contains any static assets like images or stylesheets.  The "views" directory contains templates and very little code.

Having multiple models directories meant being able to run the application on top of different database engines.  Each model would be required to support the same API, and then choice of model is controlled by the code in app.js.

That the Controller code in the routes directory had to be used with any module meant the routes modules did not directly load the model modules.  That is, routes/users.js did not require('../models-memory/users').  Instead, the model's were require'd in app.js (require('models-xyzzy/users')) and passed into the route modules in a configuration function like so:

var rUsers = require('routes/users');
rUsers.config({ model: require('models-xyzzy/users') });

The authors of Express did not, uh, express an Opinion one way or the other, on what's the Best Practice in organizing the modules in an Express application.  At the end of the day it's up to you.  But, you're almost certainly working in a team with other developers.  The group of you need to work together, so your team needs to be on the same page on these decisions.  As far as determining a best practice, it's better to follow whatever decision your team settles on.