András Tóth Full-Stack Developer at RisingStack.

Project structuring is an important topic because the way you bootstrap your application can determine the whole development experience throughout the life of the project.

In this Node.js project structure tutorial I’ll answer some of the most common questions we receive at RisingStack about structuring advanced Node applications, and help you with structuring a complex project.

These are the goals that we are aiming for:

Writing an application that is easy to scale and maintain.

The config is well separated from the business logic.

Our application can consist of multiple process types.

Node.js at Scale is a collection of articles focusing on the needs of companies with bigger Node.js installations and advanced Node developers. Chapters:

The Node.js Project Structure

Our example application is listening on Twitter tweets and tracks certain keywords. In case of a keyword match, the tweet will be sent to a RabbitMQ queue, which will be processed and saved to Redis. We will also have a REST API exposing the tweets we have saved.

You can take a look at the code on GitHub. The file structure for this project looks like the following:

. |-- config | |-- components | | |-- common.js | | |-- logger.js | | |-- rabbitmq.js | | |-- redis.js | | |-- server.js | | `-- twitter.js | |-- index.js | |-- social-preprocessor-worker.js | |-- twitter-stream-worker.js | `-- web.js |-- models | |-- redis | | |-- index.js | | `-- redis.js | |-- tortoise | | |-- index.js | | `-- tortoise.js | `-- twitter | |-- index.js | `-- twitter.js |-- scripts |-- test | `-- setup.js |-- web | |-- middleware | | |-- index.js | | `-- parseQuery.js | |-- router | | |-- api | | | |-- tweets | | | | |-- get.js | | | | |-- get.spec.js | | | | `-- index.js | | | `-- index.js | | `-- index.js | |-- index.js | `-- server.js |-- worker | |-- social-preprocessor | | |-- index.js | | `-- worker.js | `-- twitter-stream | |-- index.js | `-- worker.js |-- index.js `-- package.json

In this example we have 3 processes:

twitter-stream-worker : The process is listening on Twitter for keywords and sends the tweets to a RabbitMQ queue.

: The process is listening on Twitter for keywords and sends the tweets to a RabbitMQ queue. social-preprocessor-worker : The process is listening on the RabbitMQ queue and saves the tweets to Redis and removes old ones.

: The process is listening on the RabbitMQ queue and saves the tweets to Redis and removes old ones. web : The process is serving a REST API with a single endpoint: GET /api/v1/tweets?limit&offset .

We will get to what differentiates a web and a worker process, but let's start with the config.

How to handle different environments and configurations?

Load your deployment specific configurations from environment variables and never add them to the codebase as constants. These are the configurations that can vary between deployments and runtime environments, like CI, staging or production. Basically, you can have the same code running everywhere.

A good test for whether the config is correctly separated from the application internals is that the codebase could be made public at any moment. This means that you can be protected from accidentally leaking secrets or compromising credentials on version control.

The environment variables can be accessed via the process.env object. Keep in mind that all the values have a type of String , so you might need to use type conversions.

// config/config.js 'use strict' // required environment variables [ 'NODE_ENV', 'PORT' ].forEach((name) => { if (!process.env[name]) { throw new Error(`Environment variable ${name} is missing`) } }) const config = { env: process.env.NODE_ENV, logger: { level: process.env.LOG_LEVEL || 'info', enabled: process.env.BOOLEAN ? process.env.BOOLEAN.toLowerCase() === 'true' : false }, server: { port: Number(process.env.PORT) } // ... } module.exports = config

Config validation

Validating environment variables is also a quite useful technique. It can help you catching configuration errors on startup before your application does anything else. You can read more about the benefits of early error detection of configurations by Adrian Colyer in this blog post.

This is how our improved config file looks like with schema validation using the joi validator:

// config/config.js 'use strict' const joi = require('joi') const envVarsSchema = joi.object({ NODE_ENV: joi.string() .allow(['development', 'production', 'test', 'provision']) .required(), PORT: joi.number() .required(), LOGGER_LEVEL: joi.string() .allow(['error', 'warn', 'info', 'verbose', 'debug', 'silly']) .default('info'), LOGGER_ENABLED: joi.boolean() .truthy('TRUE') .truthy('true') .falsy('FALSE') .falsy('false') .default(true) }).unknown() .required() const { error, value: envVars } = joi.validate(process.env, envVarsSchema) if (error) { throw new Error(`Config validation error: ${error.message}`) } const config = { env: envVars.NODE_ENV, isTest: envVars.NODE_ENV === 'test', isDevelopment: envVars.NODE_ENV === 'development', logger: { level: envVars.LOGGER_LEVEL, enabled: envVars.LOGGER_ENABLED }, server: { port: envVars.PORT } // ... } module.exports = config

Config splitting

Splitting the configuration by components can be a good solution to forego a single, growing config file.

// config/components/logger.js 'use strict' const joi = require('joi') const envVarsSchema = joi.object({ LOGGER_LEVEL: joi.string() .allow(['error', 'warn', 'info', 'verbose', 'debug', 'silly']) .default('info'), LOGGER_ENABLED: joi.boolean() .truthy('TRUE') .truthy('true') .falsy('FALSE') .falsy('false') .default(true) }).unknown() .required() const { error, value: envVars } = joi.validate(process.env, envVarsSchema) if (error) { throw new Error(`Config validation error: ${error.message}`) } const config = { logger: { level: envVars.LOGGER_LEVEL, enabled: envVars.LOGGER_ENABLED } } module.exports = config

Then in the config.js file we only need to combine the components.

// config/config.js 'use strict' const common = require('./components/common') const logger = require('./components/logger') const redis = require('./components/redis') const server = require('./components/server') module.exports = Object.assign({}, common, logger, redis, server)

You should never group your config together into "environment" specific files, like config/production.js for production. It doesn't scale well as your app expands into more deployments over time.

How to organize a multi-process application?

The process is the main building block of a modern application. An app can have multiple stateless processes, just like in our example. HTTP requests can be handled by a web process and long-running or scheduled background tasks by a worker. They are stateless, because any data that needs to be persisted is stored in a stateful database. For this reason, adding more concurrent processes are very simple. These processes can be independently scaled based on the load or other metrics.

In the previous section, we saw how to break down the config into components. This comes very handy when having different process types. Each type can have its own config only requiring the components it needs, without expecting unused environment variables.

In the config/index.js file:

// config/index.js 'use strict' const processType = process.env.PROCESS_TYPE let config try { config = require(`./${processType}`) } catch (ex) { if (ex.code === 'MODULE_NOT_FOUND') { throw new Error(`No config for process type: ${processType}`) } throw ex } module.exports = config

In the root index.js file we start the process selected with the PROCESS_TYPE environment variable:

// index.js 'use strict' const processType = process.env.PROCESS_TYPE if (processType === 'web') { require('./web') } else if (processType === 'twitter-stream-worker') { require('./worker/twitter-stream') } else if (processType === 'social-preprocessor-worker') { require('./worker/social-preprocessor') } else { throw new Error(`${processType} is an unsupported process type. Use one of: 'web', 'twitter-stream-worker', 'social-preprocessor-worker'!`) }

The nice thing about this is that we still got one application, but we have managed to split it into multiple, independent processes. Each of them can be started and scaled individually, without influencing the other parts. You can achieve this without sacrificing your DRY codebase, because parts of the code, like the models, can be shared between the different processes.

How to organize your test files?

Place your test files next to the tested modules using some kind of naming convention, like <module_name>.spec.js and <module_name>.e2e.spec.js . Your tests should live together with the tested modules, keeping them in sync. It would be really hard to find and maintain the tests and the corresponding functionality when the test files are completely separated from the business logic.

A separated /test folder can hold all the additional test setup and utilities not used by the application itself.

Where to put your build and script files?

We tend to create a /scripts folder where we put our bash and node scripts for database synchronization, front-end builds and so on. This folder separates them from your application code and prevents you from putting too many script files into the root directory. List them in your npm scripts for easier usage.

Conclusion

I hope you enjoyed this article on project structuring. I highly recommend to check out our previous article on the subject, where we laid out the 5 fundamentals of Node.js project structuring.

If you have any questions, please let me know in the comments. In the next chapter of the Node.js at Scale series, we’re going to dive deep into JavaScript clean coding. See you next week!