Layout of my Node.js + Express Project

One thing I like about Express is that it provides the most important features for web development with a very smooth learning curve. (Of course a lot of credit should also go to Connect middleware on top of which Express runs) This is possible thanks to Express having very few conventions about how to layout your files in your project. Express leaves the decision to you.

I’ve created the following directory structure for my toy project:


The app.js is the entry point for the web application. app.js does not do too much stuff itself. It sets the environment and registers the controllers I want to use:

var app = express.createServer(..);

Controllers are saved under the /controllers directory. They all set their module.exports variable to a function with one argument expecting the argument to be an Express app instance. When they get the app object, they use its get, post, put and delete functions to register themselves to different url patterns. Here is a code snippet from controllers/index.js:

module.exports = function(app) {
app.get("/", index);

function index(req, res) {

/views folder is the only convention from Express where I store the .jade template files. I like Jade although it has many places that need improvement. If I can find time I’d like to contribute to this project.

/logics stores the logic modules. I try to make them as sterile as possible, that is, I’m trying to develop them protocol agnostic. Controllers are responsible to deal with HTTP level and prepare the data and other parameters to be ready for logic modules. Logic modules usually provide functions that expect some parameters and a callback to return the results.

Another point about logics is that they are not globally accessible. That is, you should explicitly load them by calling require() function. Although you have to remember to include the necessary logic modules, it makes explicit and clear which modules a file uses. Next time, when you need to change some functionality about a logic it becomes easier to find the files using this logic module.

Finally, /models directory have my Mongoose Model modules.

3 thoughts on “Layout of my Node.js + Express Project

  1. I glad i found your blog, I have adopted the above structure but it is causing me to be little obsessive on keeping it clean. For the express configuration options are you passing them threw
    express.createserver(express.logger(), ...)

    or are you configuring threw a sudo “controller”?

    IE app.configure(function(){
    app.use(express.staticProvider(__dirname + '/public'));

Leave a Reply

Your email address will not be published. Required fields are marked *