DEV Community

loading...

Difference between Controllers, Routes and Services

Utkal
A computer science enthusiast. Interested in building efficient APIs and user friendly software applications.
Updated on ・1 min read

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 (2)

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