Node.js modular folder structure and best coding practice – part 1 – overview.

Folder structure snapshot.

Why structure needs ?

Consider you are working as a team member you want to make a uniform app folder structure in such a way your team member synchronize with each other and build reusable code quickly, debug become very easy.

It’s just easy like tom making a cigarette.

Technology Stack

Node.js, Express.js, MongoDB, Typescript, Passport, oAuth2, JSON, and microservice along with Refresh Token.

Github Repository.

Boilerplate of MERN stack with Typescript

Let’s get started.


Each module contains multiple models, for example, if we have a payment module, in this case, we might need a transaction collection, purchase collection, we can add two separate models in the payment module.


dao(Data Access Object) is a pattern here deal with the mongoose/Your-Favorite database. Now rather than directly use dao into routers or collection, this is the best practice, so we can test this module independently. And if you want to change a database you just need to build a query responsibility here and it’s ready to go, so migration will become very easy here.

Don’t: Don’t use mongoose query directly into Router or controller.

See, How the database changes like clothes.


This folder is responsible to build logic, let’s consider I want to send an email verification mail to the user, in this case.

Let’s say we have two kinds of users one Is the user and another is the seller. In this case.

if we define services/verifyMail.service.ts

class VerifyMailService {
    //Your Mail Logic Here 

This function can be use in user and seller service, so it automaticaly become a reusable component.

Don’t: Don’t create a service logic into the controller directly because we never know this code in the future will become a sharable code.

Also, don’t pass params request, response, or next of express router objects, because we want to make it reusable not personal.


This folder is responsible to handle requests and responses only.

Let’s say we have a create account request, in this case, the controller method will take input from the request, pass it to the service and respond according to service.

It’s just like, the cook is calling service by pressing a white button and responding by pressing the red square button.


This folder is responsible to deal with URL, request type, validation, and sequential operations.

For example, We are making a router sign-up. In this case, we want to validate(registrationValidationRule) user input and create an account if validation success.

Here is a code snapshot, Source Code

userRouter.route("/registration").post( registrationValidationRule, bodyValidationMiddleware.verifyBodyFieldsErrors, userController.registration);

See how it’s validating and moving ahead.

Next ?

Part 2 : Node.js modular folder structure and best coding practice – part 2 – Reusable components.


We can use this module pattern to save our time and life easier, but again there is no thumb rule we can use any folder structure, the thing is it should solve our long-term problems.

If you like my post, please comment/share/like my post this will boost me to create a new blog for you.

Leave a Reply

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