9.MVC Architecture
MVC Architecture
M - Model, Business Logic
V - View, Presentation Logic
C - Controller, Application Logic
Controller - The function of the controllers is to handle the application's request, interact with models and send back responses to the client -- Application logic
View - the website that is send back to the client.
MVC in the context of RequestResponseCycle
When request hits one of the routers(we will have routers for every resource like tourRouter.js, userRouter.js).
Now the goal of the router is to delegate the request to the correct handler function (which will be in one of the controllers, there will one controllers for each of the resources).
Depends on the incoming request, the controller might interact with one of the models (For example, to retrive some document from database or to create a new one, there will be one file for each resource).
After getting data from the model, the controller will send the response to the client with the data.
In order to render a website, after getting data from the models, the controller will select one of the view templates and inject the data into it. Then the rendered website will then be sent back as the response.
Application Vs Business Logic
Application Logic
- Code that is only concerned about the application's implmentation, not the underlying business problem we are trying to solve.
Eg: Showing and selling tours. - Concerned about managing requests and responses.
- Concerned teh app's technical aspects.
- Bride between model and view layers.
Business Logic
- Code that actually solves the business problem we set out to solve.
- Directly related to the business rules, how the business works, and business needs.
According to Fat Models/thin Controllers philosophy offload as much logic as possible into the models, and keep the controllers as simple and lean as possible.