Search code examples
javascriptnode.jsexpressmodel-view-controller

What exactly represents controller in Node.js application (MVC architecture)


I have a question about MVC architecture in my Node.js application, to be more specific - what exactly controller part is.

I know that model is basically part of the application, which works with raw data - (MongoDB models, schemas..)

View can be either rendered on server side or on the client (Angular...). But here, I also need a clarification. When I am using for example Angular as my front-end framework, does it count as a whole? Like Angular is just "view" from node.js perspective?

And what exactly represents controller? Is it the main index.js file, where I have all imports (routes, middlewares...)? If so, what are other files called - libs?

Thanks for your answers


Solution

  • The controller is the "glue" between the Model and the View. As for the rendering, you can use react,vue, angular, or whatever server-side or client-side depending on your needs.

    For example, the Customer controller will handle all the interactions and inputs from the Customer View and update the database using the Customer Model. The same controller will be used to view the Customer data.

    Tutorial Point

    In my opinion, the paradigm doesn't matter and frameworks don't fully respect it either. You should simply learn how the framework works.

    Most of the time you write an API that sends data to your frontend via AJAX (JSON) request. Your frontEnd manipulates the information to place it visually in the GUI. Sometimes you write class or function to help you formate the data received from the API. You can place those functions/classes in a lib folder.

    for the frontend part, this site can maybe help you visualize the workflow. http://todomvc.com/. But the doc of your frameworks is the best resource. Look for single file component.