관리 메뉴

Silver Library (Archived)

Nodejs - EP 6 , MVC pattern 본문

Personal DB/Unclassified record

Nodejs - EP 6 , MVC pattern

Chesed Kim 2021. 2. 19. 21:33
반응형

Note.

 

M = Data

V = How does the data look like

C = function which looks for the data

 

So, why MVC all of sudden?

To separate both 'URL' and 'function' for the sake of my mental health.

 

By separating the URL sections to (1) another file with (2) another name of folder,

 

let the folder name 'controller', and let that controller be the place to importing site for 'routers (folder)'.

 

What this mean is for example,

'globalRouter.js' includes the following

export const join = (req, res) => res.send("Join");
export const login = (req, res) => res.send("Log in");
export const logout = (req, res) => res.send("Log out");
export const users = (req, res) => res.send("Users");
export const userDetail = (req, res) => res.send("User Detail");
export const editProfile = (req, res) => res.send("Edit Profile");
export const changePassword = (req, res) => res.send("Change Password");
export const home = (req, res) => res.send("Home");
export const search = (req, res) => res.send("Search");
export const videos = (req, res) => res.send("Videos");
export const upload = (req, res) => res.send("Upload");
export const videoDetail = (req, res) => res.send("videoDetail");
export const editVideo = (req, res) => res.send("editVideo");
export const deleteVideo = (req, res) => res.send("deleteVideo");

Both codes are from 'controllers' folder, which included const variable as the URL.

(the reason why I judged them as URLs, each export shows the intention to link it to single function)

 

And what about router folder side? back to the globalRouter.js

import express from "express";
import routes from "../routes";
import { home, search } from "../controllers/videoController";
import { join, login, logout } from "../controllers/userController";

const globalRouter = express.Router();

globalRouter.get(routes.home, home);
globalRouter.get(routes.search, search);
globalRouter.get(routes.join, join);
globalRouter.get(routes.login, login);
globalRouter.get(routes.logout, logout);

export default globalRouter;

unlike messy [req, res] object lined up, now URL and function are separated but still working as intended.

 

Keep calm and compare the top and bottom code.

import express from "express";
import routes from "../routes";

const globalRouter = express.Router();

globalRouter.get(routes.home, (req, res) => res.send("Home"));
globalRouter.get(routes.join, (req, res) => res.send("Join"));
globalRouter.get(routes.login, (req, res) => res.send("Login"));
globalRouter.get(routes.logout, (req, res) => res.send("Logout"));
globalRouter.get(routes.search, (req, res) => res.send("Search"));

export default globalRouter;