DEV Community

David Sima
David Sima

Posted on

How do you separate and organize your Express.JS files?

I am currently working on a project in which I have to also create a server using express.js. I am curious to know what is the best way to organize the files. Do you extract every endpoint function into a new file? How do you group them?

Top comments (2)

ccleary00 profile image
Corey Cleary

Lack of a common project structure in the Node-world is very frustrating. Every team I've been on has done it differently, but I've combined what I learned from each project and have been able to come up with a structure I feel is pretty sound.

I structure mine like so:

  • controllers
  • db/DAL
  • routes (each endpoint)
  • services
  • tests
  • utils app.js (the entrypoint)

By adding a "services" concept, you can put a lot of your business logic there, and leave the controllers to simply take the HTTP request (req object in Express) and orchestrate the services. This makes your business logic much easier to test and keeps your controllers thin.

I wrote about this in much more detail here - Project structure for an Express REST API when there is no "standard way" and explain not only what the structure should look like, but what types of logic go there.

the24thds profile image
David Sima

Thank you! Your article is very good. That helped me a lot :)