Understanding Model-View-Controller (MVC) Architecture with Express(Nodejs)
31 May 2023 · 3 min read · first published on Medium

Note: This is a beginner-friendly introduction to MVC Architecture with Express(Nodejs).
Introduction
When building web applications, it’s important to follow a well-organized structure to maintain code maintainability and scalability. One popular architectural pattern is the Model-View-Controller (MVC) pattern. In this blog, we’ll explore how to implement the MVC architecture in an Express.js application with React as the view layer.
What is MVC?
The MVC pattern separates an application into three interconnected components: the Model, the View, and the Controller. Each component has its own responsibilities, which helps in achieving code separation and modularity.
Model
The Model represents the data and business logic of the application. It handles data manipulation, validation, and storage. Here’s an example of a User model using a dummy array to simulate data storage:
// user.js (model)
const users = [];
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
save() {
users.push(this);
}
static getAll() {
return users;
}
}
module.exports = User;
View
The View component is responsible for rendering the user interface. In our case, we’ll use React to create reusable components that represent the views. Here’s an example of a Users component:
// Users.js (view)
import React from 'react';
const Users = ({ users }) => {
return (
<div>
<h1>User List</h1>
<ul>
{users.map(user => (
<li key={user.email}>
{user.name} - {user.email}
</li>
))}
</ul>
</div>
);
};
export default Users;
Controller
The Controller acts as a bridge between the Model and the View. It handles user requests, performs necessary actions, and updates the Model or renders the appropriate View. Here’s an example of a UserController in Express:
// userController.js (controller)
const express = require('express');
const router = express.Router();
const User = require('../models/user');
// Get all users
router.get('/', (req, res) => {
const users = User.getAll();
res.json(users);
});
// Create a new user
router.post('/', (req, res) => {
const { name, email } = req.body;
const user = new User(name, email);
user.save();
res.status(201).json({ message: 'User created successfully' });
});
module.exports = router;
We can create a directory structure as follows:
- app.js
- models/
- user.js
- views/
- Users.js
- controllers/
- userController.js
In the app.js file, we can then require the necessary components and define the routes:
// app.js
const express = require('express');
const app = express();
const userController = require('./controllers/userController');
const Users = require('./views/Users');
// Set up middlewares
app.use(express.json());
app.use('/users', userController);
// Render React view
app.get('/users', (req, res) => {
const users = User.getAll();
res.send(<Users users={users} />);
});
const port = 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Implementing the Model-View-Controller (MVC) architecture in an Express.js application with React as the view layer allows for a structured and modular codebase. The Model handles data and business logic, the View renders the user interface using React components, and the Controller handles user requests and updates the Model or renders the appropriate View.
Remember, this is just a basic example to get you started. In real-world applications, you may need to handle more complex scenarios and incorporate additional components.
By adopting the MVC architecture, you can improve code organization, maintainability, and scalability, making it easier to build robust web applications.
Happy coding!
Comments and corrections are open on the original post.