node.jsmodel-view-controller

Separate routes, controller, api and models folder for nodejs


This is a pretty noob question but I'm pretty new to nodejs

My current project requires nodejs as backend and reactjs as frontend

I'm familiar with MVC structure but I dont get the api folder I've seen in my templates and projects

It goes something like this:

project_name
|_api
  |_v1
    |_some_api.js
|_model
  |_some_model.js
|_view---------------------------> (for admin dashboard and stuffs)
|_controller
  |_login.js --------------------> (handles logic like login and stuff)
  |_signup.js
|_routes ------------------------> (contains all the routes)

Why would someone need an extra api folder?

I mean shouldn't all the api be returned by the controller? Like the login logic and the corresponding json response.

My current project requires CRUD operations for two types of item. So I know I wouldn't need all this but I want to learn about nodejs and the good practices. Also pointers for my folder structure would be helpful.

N.B: I know this question might be tagged as opinion based but I haven't found any good articles(maybe I'm not looking for the right thing). So articles/links would be appreciated.


Solution

  • I mean shouldn't all the api be returned by the controller?

    API stands for "application programming interface".

    Concept is kinda simple. Controllers folder is used for managing internal logic of your application while the API folder is explicitly used for exposing endpoints to 3rd party applications (aka somebody else might call your system, not just your GUI).