0% found this document useful (0 votes)
15 views1 page

MVC Notes NodeJS

Uploaded by

Vritra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

MVC Notes NodeJS

Uploaded by

Vritra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MVC in [Link] (Express.

js)

MVC stands for **Model-View-Controller**, a design pattern used to structure applications. It


separates the code into 3 layers: - **Model** → Handles data and database logic. - **View** →
Handles UI (Frontend). - **Controller** → Handles logic, routes, and connects Model + View. In
[Link] with Express, MVC is used to keep projects **organized, scalable, and easy to maintain**.

Handles DB Business Logic UI/Frontend

Model Controller View

**Typical MVC Folder Structure in [Link]** ``` project/ ■■■ controllers/ # Logic (talks to Models,
sends response) ■■■ models/ # Database schemas (Mongoose, Sequelize etc.) ■■■ routes/ #
Define endpoints and use controllers ■■■ views/ # Frontend templates (EJS, React, etc.) ■■■
helpers/ # Utility functions (optional) ■■■ [Link] # Main entry file ```

**Example Model ([Link] in models/):** ```js const mongoose = require("mongoose"); const


productSchema = new [Link]({ name: String, price: Number, description: String });
[Link] = [Link]("Product", productSchema); ```

**Example Controller ([Link] in controllers/):** ```js const Product =


require("../models/Product"); [Link] = async (req, res) => { const products = await
[Link](); [Link](products); }; ```

**Example Route ([Link] in routes/):** ```js const express = require("express"); const


router = [Link](); const { getAllProducts } = require("../controllers/products-controller");
[Link]("/products", getAllProducts); [Link] = router; ```

**Main App ([Link]):** ```js const express = require("express"); const mongoose =


require("mongoose"); const productRoutes = require("./routes/product-routes"); const app =
express(); [Link]("mongodb://localhost:27017/mvc_demo"); [Link]([Link]());
[Link]("/api", productRoutes); [Link](5000, () => [Link]("Server running on port 5000"));
```

■ **Summary:** - Model = Database logic - View = Frontend/UI - Controller = Connects Model &
View - Routes = Entry points to Controller This structure makes applications **clean, reusable, and
easy to scale**.

You might also like