DEV Community

Utkal
Utkal

Posted on • Updated on

Difference between Controllers, Routes and Services

I am building an API service using Nodejs, Expressjs and MongoDB. Also, I am using Mongoose library to deal with database. After going through several blogs on file structuring, I am quiet confused about it. I want help with the file structuring for my project. My "src" folder structure looks like this so far :-

src
  |
  |__Controllers
  |      |
  |      |__ admin.js
  |      |__ customer.js
  |__Models
  |__Public
  |__Routes
  |      |
  |      |__ admin.js
  |      |__ customer.js
  |__Views
  |__Services
         |
         |__admin.services.js
         |__customer.services.js
Enter fullscreen mode Exit fullscreen mode

My concerns are :-
1) Are Controllers the same as Routes? If not, should they be put in Routes folder?

2) How should my Service files be structured (Am I doing this right?)? Or should each API call get a different service file?

3) Since routes are handling request and response objects, what do we use controllers for? Can we just call "services" from routes and respond from each API route, or am I leaving something behind?

Discussion (3)

Collapse
barabazs profile image
Barabazs

Controllers and routes are the same, as in they are a type of "middleware".
IIRC express.js does not explicitly mention controllers, but refer to the term "application-level middleware". And routers are "router-level middleware".

Collapse
jasonwarner profile image
Jason • Edited on

My understanding and what works for me
1.) I use controllers to control the flow of my microservice/application, calling on other modules I have created such as routes and services. A route should be used strictly for making an asynchronous request or fetching data. Often, my controller for a given microservice will call multiple routes sequentially then returning the data into a service.
Example pic of one of my controllers for a microservice: dev-to-uploads.s3.amazonaws.com/up...

2.) Services should strictly be used for business logic. Are you creating/manipulating a data structure, heavily modifying data you requested, or authenticating something? These are just a few examples of business logic you would want to extract into a service module. I sometimes confuse services with middleware, the distinction is that middleware should be able to be used by multiple modules whereas services should have data fed into them and then be doing "business" logic.

3.) Again, controllers are often controlling the flow or orchestrating it at least from the beginning, calling your routes and sending them into a service module.

All of this terminology and methodology is striving to fulfill the single-responsibility principle (SRP) through separation of concerns.

Resources that can help build an understanding:

blog.logrocket.com/the-perfect-arc...

softwareontheroad.com/ideal-nodejs...

Collapse
bam92 profile image
Abel Lifaefi Mbula

Hi,

I'd suggest you have a look here where you can find many info about Nodejs best practices.