Reputation: 1441
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
Upvotes: 2
Views: 2874
Reputation: 41
Controller is just a function!. It has two arguments. First argument is for request and it comes from client side and second argument is response and it comes from server. So controller receives two things: request and response. Working: controller takes request and modifies request and saves something into database and then if saved successfully it will return a response to the client and if something went wrong then it will send the specific error to client. Finally.... controller helps to communicate between client and database. Where client is also known as view and database is model. And i think you got what is controller or C in MVC. Flow : V to C to M !. V will make a request and C takes the request and plays with the request before querying with M.
Upvotes: 0
Reputation: 151
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.
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.
Upvotes: 3