Structuring your NodeJS app.

The modular way.

I have been using node for a while now and have come to the conclusion that there are a lot of different versions of “the right way” to architect your node app. For developers who have been using node, most have probably already gotten set in their ways and have their own boilerplate code that they use for their node based projects. That’s all well and good if you’ve discovered your own end all solution, but for beginners and people who are new to node, all of the various app structures and boilerplate code out on the interwebs can be a little intimidating.

So here is one for the new guys, a sort of go-to way to structure your app that will allow for modularity, scalability, and a logical way to set up your node apps.

File Structure:

├── api
│ ├── models
│ │ ├── project.js
│ └── routes.js
├── config
│ ├── database.js
└── server.js

Alright now let me explain the above structure. Within the root folder of your project, you will have three things: your server.js file, a folder named api, and a folder named config. The server.js file is where your node server startup code will live. I use a boilerplate piece of code for this file that looks something like this:

var express = require(‘express’);
var app = express();
var port = process.env.PORT || 3000;
var database = require(‘./config/database’);
app.use(function (error, request, response, next) {
app.listen(port, function() {
console.log(“Node app is running at localhost:” + port);

This file will start you off using express for routing, it will start up your node server on port 3000 or if you’re hosting on heroku the process.env.port will get called by them, and then it requires files from your api and config folders.

Now let’s talk about the config folder. Inside of here is where you will store your database configurations, mailer configurations if you’re sending emails via node, you’re API tokens, and so on and so forth. Here is just a brief example of a database.js file I use for a postgres database:

var Sequelize = require(‘sequelize’);
var sequelize = new Sequelize(‘development’, ‘Username’, null, {
host: ‘localhost’,
dialect: ‘postgres’,
define: {
timestamps: false,
module.exports.database = sequelize;

The key thing to take away from this file is this line:

module.exports.database = sequelize;

Basically what this line says is take that when we require the database.js file in our other files, we will have access to the database variable, which is equal to sequalize. This is the beauty of what we are doing here. Rather than have one giant node file with everything in it, we are modularizing everything out so that it is easily accessible and doesn’t make your eyes bleed trying to find that one line of code.

The final folder in our structure is the api folder. Inside we have a routes.js file, and another folder called models. Inside of the models folder, there is a file called project.js. You can obviously name this file whatever you want, mine is named project.js because it contains the model of a project. The file looks like this:

var database = require(‘../../config/database’).database;
var Project = database.define(‘projects’, {
id: { type: ‘INTEGER’, primaryKey: true },
name: { type: ‘TEXT’, allowNull: false },
description: { type: ‘TEXT’, allowNull: false },
languages: { type: ‘ARRAY’, allowNull: false },
frameworks: { type: ‘ARRAY’, allowNull: false }
module.exports.Project = Project;

As you can see, this is a database object model for my postgres database. It defines the different columns for a table called projects where I store information about the projects I work on. Now obviously this will change or be different based on whatever database you choose to connect to, but the idea is the same. Also note, that in the last line we are exporting Project. This means we will have access to this code through the variable Project.

The routes.js file is pretty self explanatory, it contains all of the routes that our node server will use. It will look something like this:

var Project = require(‘./models/project.js’).Project;
bodyParser = require(‘body-parser’);
module.exports = function(app) {
 app.get(‘/api/projects’, function(request, response) {
Project.findAll({ where: { show: ‘TRUE’ }})
.then(function(projects) {

At the top, you can see we are requiring the projects.js file, and specifying that we want the Projects variable. From there we are able to use it in our HTTP GET route and return all of the projects and their values as JSON.

There you have it! Module.export all of the things, separate your configs, your routes, and your database models from the server.js file, and continue that trend as your project grows.

Happy coding!