Pick your best Node JS Architecture

Pick your best Node JS Architecture

Have a good dream and start to refactor your nodejs application. Welcome to this article that helps you pick the right choice for your nodejs architectures.

In this article, you will learn more about

  • Split your code into different functionality

  • Know more about the file name and what you should put inside

  • Different folders approach for your nodejs app

  • When to use the folder structure

  • What to do next

Ok, with that said let's get started.


File Naming

When building your app, the most important thing you should understand is your file name. There are some approaches to naming your file, however, to make it clear this is what I did

  • Use the small case file naming. While using the small case file name, it will help your eye still consistent wherever you are looking into. Use user.js instead of User.js

  • Always separate file names using '_' (Underscore) or '.' (Dot). It will be like user_controller.js or user.controller.js. You can pick one of these rules, however, some framework defines their rule, for example, NestJS use the dot rule.

So using the correct file name will help you navigate easily when your application scales bigger, so stay consistent.


Layer Approach

The layered approach is a design pattern to divides your application code into a different layers including presentation, logic, and data. By using this pattern your code will separate into a specific purpose in each folder.

This approach works perfectly for your application if you want to keep it clean and easy to navigate each other. You will never be confused when want to change sometime in the application.

project/
├── public/
|   ├── images
├── src/
│   ├── controllers/
|   |   ├── user_controller.js
|   |   ├── auth_controller.js
|   |   ├── task_controller.js
|   ├── routes/
|   |   ├── app_route.js
|   |   ├── auth_route.js
│   ├── middleware/
|   |   ├── auth_middleware.js
│   ├── models/
|   |   ├── user_model.js
|   |   ├── profile_model.js
|   |   ├── task_model.js
│   ├── services/
|   |   ├── user_service.js
|   |   ├── tast_service.js
│   └── utils/
|   |   ├── decode_util.js
|   |   ├── time_converter_util.js
|   ├── main.js
├── test/
├── node_modules/
├── package.json
├── README.md
└── .gitignore
  • public/ Contains all of your static files including your styles, and other public files, you can also specify into a different folder.

  • src/ Contains all of your source code application

    • controllers/ Contains the logic to handle the incoming request and response

    • routes/ Contains all routes for app definitions

    • middleware/ Contain all of your middleware to modify your application request and response.

    • models/ Contain all of your database models for ORM

    • services/ Contain your application business logic and service, API integration, and vendor.

    • utils/ Contains all of the utility functions to help your application like the decoder, time converter, and so on.

    • main.js The main file that contains all bootstrap application that imports the app, service, controller, routes, and run the application

  • test/ Contain all tests file for the application

  • node_modules Contains all of the dependencies installed by npm

  • package.json Contains your app metadata, dependencies, and project info.


Modular Approach

The modular approach is the design pattern for applications that divides the code into small feature modules. This will break all of your code into a self-contained folder that easily to test, and reuse. In this approach, each module performs a specific function and communicates with another module.

This approach is very popular and very useful when you are building a big project that contains many modules and purposes. Because it is easy to scale and maintain when the project becomes bigger. To make this approach run perfectly, we can use the dependencies injection that allows us to import a module from another one.

One of the frameworks that implements this feature is 'NestJS'. they also provide the same folder structure and keep all files consistent and never miss with the purpose.

project/
├── public/
|   ├── images
├── src/
│   ├── module1(user)/
│   │   ├── user.controller.js
│   │   ├── user.service.js
│   │   ├── user.router.js
│   │   ├── user.controller.spec.js
│   │   ├── user.middleware.js
│   │   ├── models/
│   │   │   ├── user.model.js
│   │   │   ├── profile.model.js
│   │   ├── utils/
│   │   │   ├── modifier.util.js
│   │   │   ├── role.util.js
│   │   └── user.module.js
│   ├── module2(task)/
│   │   ├── task.controller.js
│   │   ├── task.service.js
│   │   ├── task.router.js
│   │   ├── task.controller.spec.js
│   │   ├── task.middleware.js
│   │   ├── task.model.js
│   │   └── task.module.js
|   └── main.js
├── node_modules/
├── package.json
├── README.md
└── .gitignore
  • public/ Contains all of your static files including your styles, and other public files, you can also specify into a different folder.

  • src/ Contains all of your source code application

    • user/ Contains all of the user module source code including service, controller, router, test, and even the utils.

    • main.js The main file to running your application, import all modules and run them together.

  • node_modules Contains all of the dependencies installed by npm

  • package.json Contains your app metadata, dependencies, and project info.

Anyway, the module folder can wrap inside a small type of folder it you have multiple files or left it alone with a single file.


Conclusion

Ok, the approach above is just a pattern, if you have something want to add just follow the same rule. So now back with the question, which one is the best? well, this depends on your project requirement, and scope.

If you want to build something that is prepared to become a large-scale project for example big commerce, microservices use the 'modular approach', however, if you want to build a project that is small, and medium scope and use 'Layer Approach' may be suited to your choice.

Once again, this is just one example, you also consider with your development team and always make it consistent. So which one is your favorite? let me know in the comment below.