DEV Community

Discussion on: Difference between Controllers, Routes and Services

Collapse
 
jasonwarner profile image
Jason • Edited

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...