NodeJs Application Structure

In this post I will show how I organize my node.js applications. I’ll start explaning the nodejs principles, with examples.

NodeJs Principles

NodeJs follows the next principles:

Unix philosophy

  • “Small is beautiful”
  • “Make each program do one thing well”

Keep It Simple, Stupid (KISS) principle.

“Simplicity is the ultimate sophistication.” – Leonardo da Vinci

I think I already gave the idea of the way to structuring a node application. Creating microservices to do specific work, separating the complexity and decoupling the responsibilities.

One of the biggest problems that people have with Nodejs is the maintainability of the application. That’s the reason why is better to have 3 different micro applications than a big one.

Project Structure

This is a simple Web api using expressjs.

File Struct

config: Application configurations for each different environment (production, dev, tests).

app: Folder with the application core. In this case we only have controllers and models.

libs: Local libraries.

  • logger – Winston wrapper for application logs.
  • authentication – Authentication with JWT.
  • cluster – Cluster application to take advantage of multi-core systems.

tests: Contains Unit, Integration, and UI tests

Gruntfile: Grunt tasks. (Jshint, Node-inspector, Nodemon, mocha)

You can get the code on Github.

Thanks for reading, if you have any suggestions I’ll be glad to hear and discuss them with you.

I'm a Software Engineer that believes software must be simple, intuitive and reliable. My main interests are api’s design, best software practices, distributed systems and continuous integration.

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

4 thoughts on “NodeJs Application Structure”

    Also for a bit more decoupling I prefer to have separate directories for `helper` and `lib`.

    Sandeep Pandey says:

    I would recommend adding middleware and services hierarchy under lib. since sometimes authorization would not be as simple to manage in single js file. Also any external service layer required would clutter the lib

    This is a simple use case but yes makes sense to have multiple directories in library, because sometimes libraries can be more complex. An example is If we want to use Passport with multiple strategies, is better to have passport folder in lib.

    Thanks for the comments 🙂

    While that’s a nice overall introduction to folder structure for node projects – are you going to further elaborate and compare that with other frameworks? most people I assume will kickstart their projects with something like SailsJS, MEAN.JS and alike…