Application Structure Setup – Node.js API with TDD Tutorial

One of the factors that either complicate or simplifies the application maintenance over the time is Application Structure. If we get this done correctly, it will be easier to be maintained by the future team that’s looking after it.

Otherwise, it will become very complicated and cumbersome to do so and also increases the maintenance cost and decreased productivity for further enhancements.

There are many ways for structuring NodeJS app available throughout the internet. Many suggest one approach or the other as each one has pros and cons. In this tutorial, we will be using one of the structure that I have been using for years. 

This approach gives a more modular structure when the features and modules are increasing over time and also increases better maintenance of the application.

Modular Approach

For our app, we will follow the below structure. We will be adding below directories in the base application that we prepared in the previous sections.

1. modules directory

In this directory, all the API specific code will be created within their subdirectory. For example, to create the scripts for creating a MongoDB connection, we will create ‘MongoDB’. Like this, we will also create a directory ‘customer’ for customer API scripts.

2. tests directory

Since this tutorial’s primary goal is to showcase the RESTful service development with the test-driven development approach, we need to maintain the test scripts for all the modules that are developed. So it makes sense to keep the same modular approach for tests scripts as well.

This decision will result in creating a directory called ‘tests’ along with ‘modules‘. Under this tests directory, we will have two more directories too. One directory is for unit testing scripts and another for the integration testing scripts.

One of the best practices is to keep the test data for running the unit and integration tests along with the source code. So we would need one more directory ‘fixtures’ for storing any test data for the test scripts.

So, after adding the directories mentioned above, the base app structure will look just like the below image.

Node.js RESTful API - Application Structure
Node.js RESTful API – Application Structure

Please check out the index page for this Node.js RESTful API development with TDD approach tutorial with all the posts in sequence in one place.

This blog post is an excerpt from the book Building Node.js REST API with TDD approach. Please check out the link for more information.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.