DEV Community

Slim Coder
Slim Coder

Posted on

Node.js Folder Structure

Hi guy's,

In my current node.js it's Microservices based project,

I'm struggling with folder structure of my boilerplate.

My current boilerplate is something like this:

Main folder is src which consist of different folders (components, bin, helpers,tests).

...components
.............goods
.................goods.controller.js
.................goods.route.js
,................goods.services.js
.................goods.plugins.js
.................goods.repository.js
...bin
...routes
...helpers
...tests

I have not faced any problem yet while using this folder structure.

We have not done seperation of responsibilities principle from starting of this project.

What you think about this boilerplate?

Oldest comments (7)

Collapse
 
asamolion profile image
Muhammad Osama Arshad

Can you give descriptions for what you're files do?

what's the roles of

goods.services.js
goods.plugins.js
good.repository.js?

Collapse
 
slimcoder profile image
Slim Coder

controller is c from MVC.
plugins consists of decorators.
repository calls services.
services calls db.
mediators handle complexity of controller.
validations file consist of joi validation.
route consist of routers.

I.E
goods.controller.js
goods.mediator.js
goods.plugins.js
goods.repository.js
goods.services.js
goods.validations.js
goods.routes.js

Collapse
 
asamolion profile image
Muhammad Osama Arshad • Edited

Cool.

Here are my thoughts:

  1. The folder structure is expressive enough to handle most cases, however I find that most of this stuff is unnecessary in small to medium scale applications. For larger applications I suppose that many layers may make sense.
  2. I see that you've grouped together all of the feature related code, however I don't see a goods.test.js file. is the by design?
  3. For most small to medium scale applications, a simple service layer and database model layer is sufficient, so this folder structure would be overkill in those cases.
Thread Thread
 
slimcoder profile image
Slim Coder

I also write test.
I was thinking about changing it to follow SRP and then DI and make a factory within each and every responsibilities.

Thread Thread
 
asamolion profile image
Muhammad Osama Arshad

What's SRP and DI?

Thread Thread
 
slimcoder profile image
Slim Coder • Edited

SRP: Seperation of Responsibility Principle.
DI: Dependency Injection

Thread Thread
 
asamolion profile image
Muhammad Osama Arshad

and these are used for?