Understanding the MVC Architecture in the MERN Stack
The Model-View-Controller (MVC) is an architectural pattern that separates an application into three basic components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application. MVC is one of the most frequently used industry-standard web development framework to create scalable and extensible projects.
Lets discuss about the three basic logic components in a little bit more detail:
- Model:
- manages the behavior and the data of the application domain.
- responds to requests for information about its state (usually from the view).
- responds to instructions for state change (usually from the controller).
- In event — driven systems, the model notifies observers (usually views) when the information changes so that they can react.
2. View:
- renders the model into a form suitable for interaction, typically a user interface element.
- Multiple views can exist for a single model for various purposes.
- A viewport typically has one to one correspondence with a display surface and knows how to render to it.
3. Controller:
- receives user input and initiates a response by making calls on model objects.
- A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.
Brief Introduction to the MERN Stack
The MERN Stack is a JavaScript Stack that is used for easier and faster deployment of full-stack web applications.
The MERN stack consists of the following technologies:
- MongoDB: A document-based open source database.
- Express: A web application framework for Node.js.
- React: A JavaScript front-end library for building user interfaces.
- Node.js: JavaScript run-time environment that executes JavaScript code outside of a browser (such as a server).
It is also common to use Mongoose, which is a simple, schema-based solution to model application data.
MVC and MERN
MERN isn’t an MVC framework in the traditional sense, because it spans both the server and the browser. MVC can also be achieved by sticking to server-side templating in Node, and just sending the HTML to the browser and treating the browser as the View layer i.e. doing things the way things have always been done forever.
Although, when React is combined with a javascript backend, then it is MVC. React serves as the “V” in the MVC
- Mongoose’ models defines the data part. This is where we will store all of the crucial data our application needs to function.
- Express & Node.js does all the functional programming and will be used to write the Business Logic Tier (controller). This tier represents the Application Server that will act as a bridge of communication for the Client and Database. This tier will serve the React components to the user’s device and accept HTTP requests from the user and follow with the appropriate response.
- React serves as the “V” in the MVC. Our Client tier (View) will be written in JavaScript, HTML, and CSS, using ReactJS as the framework. This level of the architecture is what the user will interact with to access the features of our application.
With this, we come come to the end of this article. in this article, we have discuss how the traditional MVC architecture is implemented in the very trending MERN Stack.
Thanks for reading!