model view controller code example

Model-View-Controller Code Example

By David White
AD

The Model-View-Controller (MVC) is a software architectural pattern that separates an application into three main components: Model, View, and Controller. This design pattern is extensively used in web development, particularly with JavaScript-based frameworks like React and Angular. Let's dive into the MVC architecture and explore a code example to understand it better.

The MVC pattern decouples an application's data (Model), user interface (View), and the control flow (Controller) that binds them together. It allows for better maintainability, testability, and scalability of the application. Now, let's see a simple example using JavaScript and Node.js.

MVC Components

Before jumping into the code example, let's briefly discuss each component in the MVC pattern.

AD
a diagram showing the flow of interaction between two different types of web pages and an app
a diagram showing the flow of interaction between two different types of web pages and an app
MVC
MVC
MVC Architecture in MERN Stack Explained (2025 Guide for Beginners)
MVC Architecture in MERN Stack Explained (2025 Guide for Beginners)

Model

The Model represents the data and the business logic of your application. It interacts with the database or other data sources and communicates changes to the View. In our example, let's create a simple 'Task' model.

View

The View is responsible for presenting the data to the user. It defines how the data should be displayed, independent of how the data is retrieved. In a web application, the View is essentially the HTML, CSS, and JavaScript that create the user interface.

a block diagram showing the different types of data flowchaps and how to use them
a block diagram showing the different types of data flowchaps and how to use them
Learn ASP.NET MVC 5 ( Model view controller) step by step in 2 days ( 16 hours)
Learn ASP.NET MVC 5 ( Model view controller) step by step in 2 days ( 16 hours)
Creating a Basic Laravel 5 MVC Application in 10 Minutes – Self-Taught Coders
Creating a Basic Laravel 5 MVC Application in 10 Minutes – Self-Taught Coders

MVC in Action

Now, let's create a simple to-do list application using the MVC pattern. We'll use Express.js for the backend and EJS (Embedded JavaScript templates) for the View.

Setting up the project

First, initialize a new Node.js project and install the required dependencies: express, ejs, and uuid (for generating unique task IDs).

AD

Creating the Model

Create a 'task.js' file in a 'models' folder. This will be our simple 'Task' model with an identifier and a description.

Spring MVC Interceptor with Example - Dinesh on Java
Spring MVC Interceptor with Example - Dinesh on Java
What is Laravel?
What is Laravel?
the table is full of names and numbers for different types of items in each region
the table is full of names and numbers for different types of items in each region
PHP MVC With Database (Very Simple Beginner Example)
PHP MVC With Database (Very Simple Beginner Example)
Guys pls send pics of your models😭😭 (Mine pls don’t repost) #whisper
Guys pls send pics of your models😭😭 (Mine pls don’t repost) #whisper
Easy Model Building with PerceptiLabs - Interactive TensorFlowVisualization GUI | Towards Data Science
Easy Model Building with PerceptiLabs - Interactive TensorFlowVisualization GUI | Towards Data Science
```javascript const { v4: uuidv4 } = require('uuid'); let tasks = []; class Task { static addTask(description) { const id = uuidv4(); tasks.push({ id, description }); } static getTasks() { return tasks; } static deleteTask(id) { tasks = tasks.filter((task) => task.id !== id); } } ```

Creating the Controller

In our 'app.js', we'll handle tasks like adding, retrieving, and deleting tasks in the 'Controller'.

```javascript const express = require('express'); const app = express(); const task = require('./models/task'); app.set('view engine', 'ejs'); app.get('/', (req, res) => { const tasks = task.getTasks(); res.render('index', { tasks }); }); app.post('/task', (req, res) => { const description = req.body.description; task.addTask(description); res.redirect('/'); }); app.delete('/ task/:id', (req, res) => { const id = req.params.id; task.deleteTask(id); res.send('Task deleted'); }); ```

Creating the View

Create an 'index.ejs' file in a 'views' folder. This will be our simple View displaying a form to add tasks and a list of added tasks.

AD
```html Simple To-Do List

Add Task

Tasks

    <% tasks.forEach(task => { %>
  • <%= task.description %>
  • <% }); %>
```

With this simple example, we've demonstrated how the MVC pattern can help in organizing and structuring an application, improving its maintainability and testability. Understanding and correctly implementing the MVC architecture is a crucial step towards building scalable and efficient web applications.

Useful Blender Hotkeys for 3D Viewport
Useful Blender Hotkeys for 3D Viewport
AD