Model-View-Controller (MVC) Architecture

 Model-View-Controller (MVC) Architecture: MVC is a popular web architecture that separates the application into three components: the model, the view, and the controller. The model represents the data and business logic of the application, the view represents the user interface, and the controller handles user input and interacts with the model and view. Here is an example of an MVC application using the Express framework:

javascript


// server.js

const express = require('express');

const app = express();


const users = [

  { id: 1, name: 'John' },

  { id: 2, name: 'Jane' },

  { id: 3, name: 'Bob' },

];


app.set('view engine', 'ejs');


app.get('/', (req, res) => {

  res.render('index', { title: 'Users', users });

});


app.listen(3000, () => {

  console.log('Server running at http://localhost:3000/');

});

In this code, we create an Express app and define a route that renders an EJS template. We pass some data to the template, including the title of the page and an array of users.


php


<!-- views/index.ejs -->

<!DOCTYPE html>

<html>

<head>

  <title><%= title %></title>

</head>

<body>

  <h1><%= title %></h1>

  <ul>

    <% users.forEach(user => { %>

      <li><%= user.name %></li>

    <% }) %>

  </ul>

</body>

</html>

In this code, we create an EJS template that displays the title of the page and a list of users.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...