DEV Community


Difference between Controllers, Routes and Services

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

  |      |
  |      |__ admin.js
  |      |__ customer.js
  |      |
  |      |__ admin.js
  |      |__ customer.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)

barabazs profile image

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

bam92 profile image
Abel Lifaefi Mbula


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