MVC stands for Model–View–Controller. It is a way to organize an application by separating responsibilities into three parts.
User
↓
Controller
↙ ↘
Model View
↓ ↓
Database User sees UI1. Model
The Model handles the application’s data and business rules.
For example:
User
- id
- name
- emailIt might communicate with the database:
Model → PostgreSQL2. View
The View is what the user sees.
For a web application:
HTML
CSS
UI componentsFor example:
┌─────────────────────┐
│ Welcome, Reyhaneh │
│ │
│ [ View Profile ] │
└─────────────────────┘3. Controller
The Controller handles the request/action and coordinates the other parts.
For example:
GET /users/123The controller might:
Request
↓
UserController
↓
UserModel
↓
Database
↓
UserController
↓
ResponseExample:
async function getUser(req, res) {
const user = await User.findById(req.params.id);
res.json(user);
}In a backend application
You might have:
src/
├── controllers/
│ └── userController.js
├── models/
│ └── userModel.js
├── routes/
│ └── userRoutes.js
└── app.jsThe flow could be:
HTTP Request
↓
Route
↓
Controller
↓
Model
↓
Database
↓
Controller
↓
HTTP ResponseImportant distinction
MVC is mainly about separation of responsibilities.
Instead of putting everything here:
app.get("/users/:id", async (req, res) => {
// validation
// business logic
// database query
// formatting response
// ...
});you separate those responsibilities into appropriate parts.
Also, MVC is an architectural pattern, not a requirement for REST APIs. You can build a REST API using MVC, but REST itself doesn’t require MVC.