Architecture
- MVC
- CQRS
- Microservice
- ...
MVC Architecture
Kacper Walczak · 05-05-2024
Lets learn how to use MVC architecture in your project.
What is MVC?
Model View Controller - is a pattern to separate concerns in the application. It is a design pattern that separates the application into three main components: Model, View, and Controller.
- Model - represents the data, the business logic, and the rules of the application.
- View - represents the user interface, the presentation layer of the application.
- Controller - acts as an interface between Model and View, it receives the input from the user and processes it (request/response).
Examples
File structure
- index.html
- document.model.js
- document.controller.js
- index.js
ExpressJS
HTML template code:
<h1>{{ title }}</h1>
<p>{{ message }}</p>Backend server serving the template:
app.get('/:documentID', (req, res) => {
const { documentID } = req.params
const isAllowedToView = req.user.isAllowedToView(documentID)
if (!isAllowedToView) {
res.status(403).send('You are not allowed to view this document')
return
}
const document = Document.findById(documentID) // fetch Document from DB
res.render('index', { title: document.title, message: document.message })
})
Documentis our model,html templateis our view, and the controller is thedocument.controller.js.
When to use MVC?
Use MVC when:
Team
You have only one team that will be maintaining this project.
Velocity
You want to produce and start a project quickly.
Purpose
Your goal is to set up a simple project that needs to serve some business purposes.
Next
In this article we have learned how to and when utilize MVC architecture.
Check
Web Architecturesto learn more about different architectures.
Check next:
READ
Latest readings
Readings are sites which will help you with detailed
information about given topic. Read latest ones from Learn.
06-03-2026
Build your own local voice assistant powered by Ollama.
06-03-2026
Generate YouTube thumbnails with FastAPI and Ollama.
05-09-2024
Compare Neo4j and Tigergraph databases, which is easier to work with, etc.