MVC (Model-View-Controller)

MVC : هى طريقة لتنظيم وترتيب ملفات المشروع لكى تُسهل عملية تعديل المشروع فى المُستقبل وتقسيم المشروع الى أجزاء صغيرة وبالتالى التحكم فى كل جزء من أجزاء الموقع بسهولة

ترتيب إنتقال الأكواد

app.js 👉 routes 👉 controllers

الأكواد المُستخدمة فى الفيديو
in controllers file :
 
// To import model & schema
const Article = require("../models/articleSchema");
 
const article_index_get = (req, res) => {
  Article.find()
    .then((result) => {
      res.render("index", { mytitle: "HOME", arrArticle: result });
    })
    .catch((err) => {
      console.log(err);
    });
};
 
const article_post = (req, res) => {
  const article = new Article(req.body);
 
  article
    .save()
    .then((result) => {
      res.redirect("/all-articles");
    })
    .catch((err) => {
      console.log(err);
    });
};
 
const article_details_get = (req, res) => {
  Article.findById(req.params.id)
    .then((result) => {
      res.render("details", { mytitle: "ARTICLE DETAILS", objArticle: result });
    })
    .catch((err) => {
      console.log(err);
    });
};
 
const article_delete = (req, res) => {
  Article.findByIdAndDelete(req.params.id)
 
    .then((params) => {
      res.json({ mylink: "/all-articles" });
    })
 
    .catch((err) => {
      console.log(err);
    });
};
 
module.exports = {
  article_index_get,
  article_post,
  article_details_get,
  article_delete,
}; 
in routes file :
 
const express = require("express");
const router = express.Router();
 
// To import controllers file
const articleController = require("../controllers/articleController");
 
// PATH start with '/all-articles'
 
router.get("/", articleController.article_index_get);
 
router.post("/", articleController.article_post);
 
router.get("/:id", articleController.article_details_get);
 
router.delete("/:id", articleController.article_delete);
 
module.exports = router; 
in app.js :
 
// To import routes file
const allArticlesRouter = require("./routes/all-articles");
 
// all-articles PATH
app.use("/all-articles", allArticlesRouter); 
المصادر
الفيديو
تحميل الملفات
😡
انت مشغل الـ AdBlock !!

ياريت تقفله لوسمحت 😊 😊

تنبيه هام ✋

إذا كانت الإعلانات مزعجة بالنسبة لك، فيُمكنك التبرع ب50$ وسيتم إيقاف الإعلانات لمدة شهر لجميع زوار الموقع 🧡 ويُمكنك التواصل معنا عن طريق صفحة الفيس بوك