Flare-k

MVC Patterns

...@@ -3,23 +3,22 @@ import morgan from "morgan"; ...@@ -3,23 +3,22 @@ import morgan from "morgan";
3 import helmet from "helmet"; 3 import helmet from "helmet";
4 import cookieParser from "cookie-parser"; 4 import cookieParser from "cookie-parser";
5 import bodyParser from "body-parser"; 5 import bodyParser from "body-parser";
6 - 6 +import userRouter from "./routers/userRouter";
7 +import videoRouter from "./routers/videoRouter";
8 +import globalRouter from "./routers/globalRouter";
9 +import routes from "./routes";
7 const app = express(); 10 const app = express();
8 -const PORT = 80;
9 -
10 -function handleListening() {
11 - console.log(`Listening on: http://localhost:${PORT}`);
12 -}
13 -
14 -const sayHello = (req, res) => res.send("Hello~!");
15 11
16 12
17 app.use(cookieParser()); 13 app.use(cookieParser());
18 app.use(bodyParser.json()); 14 app.use(bodyParser.json());
19 -app.use(bodyParser.urlencoded({ extended: true })); 15 +app.use(bodyParser.urlencoded({ extended: true })); //json, html, text, urlencoded 할 거 없이 다 parser할 수 있도록 설정해줘야 한다.
20 app.use(helmet()); 16 app.use(helmet());
21 app.use(morgan("dev")); 17 app.use(morgan("dev"));
22 18
23 -app.get('/', sayHello);
24 19
25 -app.listen(PORT, handleListening);
...\ No newline at end of file ...\ No newline at end of file
20 +app.use(routes.home, globalRouter);
21 +app.use(routes.users, userRouter);
22 +app.use(routes.videos, videoRouter);
23 +
24 +export default app; //파일을 불러올때 app object를 준다는 의미.
...\ No newline at end of file ...\ No newline at end of file
......
1 +export const join = (req, res) => res.send("join");
2 +export const login = (req, res) => res.send("login");
3 +export const logout = (req, res) => res.send("logout");
4 +export const users = (req, res) => res.send("users");
5 +export const userDetail = (req, res) => res.send("user Detail");
6 +export const editProfile = (req, res) => res.send("Edit Profile");
7 +export const changePassword = (req, res) => res.send("Change Password");
...\ No newline at end of file ...\ No newline at end of file
1 +export const home = (req, res) => res.send("home");
2 +export const search = (req, res) => res.send("search");
3 +export const videos = (req, res) => res.send("videos");
4 +export const upload = (req, res) => res.send("upload");
5 +export const videoDetail = (req, res) => res.send("videoDetail");
6 +export const editVideo = (req, res) => res.send("editVideo");
7 +export const deleteVideo = (req, res) => res.send("deleteVideo");
...\ No newline at end of file ...\ No newline at end of file
1 +import app from "./app"; // app.js에서 export default app했기 때문에 불러올 수 있다.
2 +
3 +const PORT = 80;
4 +
5 +const handleListening = () => {
6 + console.log(`-> Listening on: http://localhost:${PORT}`);
7 + //call-back함수.
8 + //PORT를 listen하기 시작할 때 함수를 호출해준다.
9 +}
10 +
11 +app.listen(PORT, handleListening);
...\ No newline at end of file ...\ No newline at end of file
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
2 "name": "myyoutube", 2 "name": "myyoutube",
3 "version": "1.0.0", 3 "version": "1.0.0",
4 "description": "make Youtube Website", 4 "description": "make Youtube Website",
5 - "main": "index.js", 5 + "main": "app.js",
6 "scripts": { 6 "scripts": {
7 - "start": "nodemon --exec babel-node index.js --delay 2" 7 + "start": "nodemon --exec babel-node init.js --delay 2"
8 }, 8 },
9 "repository": { 9 "repository": {
10 "type": "git", 10 "type": "git",
......
1 +import express from "express";
2 +import routes from "../routes";
3 +import { home, search } from "../controllers/videoController";
4 +import { join, login, logout } from "../controllers/userController";
5 +
6 +const globalRouter = express.Router();
7 +
8 +globalRouter.get(routes.home, home);
9 +globalRouter.get(routes.join, join);
10 +globalRouter.get(routes.login, login);
11 +globalRouter.get(routes.logout, logout);
12 +globalRouter.get(routes.search, search);
13 +export default globalRouter;
...\ No newline at end of file ...\ No newline at end of file
1 +import express from "express";
2 +import routes from "../routes";
3 +import { users, userDetail, editProfile, changePassword } from "../controllers/userController";
4 +
5 +const userRouter = express.Router();
6 +
7 +
8 +userRouter.get(routes.users, users);
9 +userRouter.get(routes.userDetail, userDetail);
10 +userRouter.get(routes.editProfile, editProfile);
11 +userRouter.get(routes.changePassword, changePassword);
12 +
13 +export default userRouter;
14 +
15 +
16 +
17 +
18 +/*
19 +userRouter.get("/", (req, res) => res.send("user index"));
20 +userRouter.get("/edit", (req, res) => res.send("user edit"));
21 +userRouter.get("/password", (req, res) => res.send("user password"));
22 +
23 +여기서 원하는 라우터를 만들고 app.js에다가
24 +app.use("/user", userRouter)
25 +이런식으로 사용하면 app.js에서 하나하나 라우터를 만드는 방법과 달리
26 +/user라 필요한 라우터에 대한 라우터들을 모두 import할 수 있게 된다..
27 +(ex, /user, /user/edit, /user/password ...)
28 +*/
...\ No newline at end of file ...\ No newline at end of file
1 +import express from "express";
2 +import routes from "../routes";
3 +import { videos, upload, videoDetail, editVideo, deleteVideo } from "../controllers/videoController";
4 +
5 +//export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다.
6 +const videoRouter = express.Router();
7 +
8 +videoRouter.get(routes.videos, videos);
9 +videoRouter.get(routes.upload, upload);
10 +videoRouter.get(routes.videoDetail, videoDetail);
11 +videoRouter.get(routes.editVideo, editVideo);
12 +videoRouter.get(routes.deleteVideo, deleteVideo);
13 +
14 +
15 +export default videoRouter;
...\ No newline at end of file ...\ No newline at end of file
1 +//Global
2 +const HOME = "/";
3 +const JOIN = "/join";
4 +const LOGIN = "/login";
5 +const LOGOUT = "/logout";
6 +const SEARCH = "/search";
7 +
8 +// Users
9 +const USERS = "/users";
10 +const USER_DETAIL = "/:id";
11 +const EDIT_PROFILE = "/edit-profile";
12 +const CHANGE_PASSWORD = "/change-password";
13 +
14 +// Videos
15 +const VIDEOS = "/videos";
16 +const UPLOAD = "/upload";
17 +const VIDEO_DETAIL = "/:id";
18 +const EDIT_VIDEO = "/:id/edit";
19 +const DELETE_VIDEO = "/:id/delete";
20 +
21 +const routes = {
22 + home: HOME,
23 + join: JOIN,
24 + login: LOGIN,
25 + logout: LOGOUT,
26 + search: SEARCH,
27 + users: USERS,
28 + userDetail: USER_DETAIL,
29 + editProfile: EDIT_PROFILE,
30 + changePassword: CHANGE_PASSWORD,
31 + videos: VIDEOS,
32 + upload: UPLOAD,
33 + videoDetail: VIDEO_DETAIL,
34 + editVideo: EDIT_VIDEO,
35 + deleteVideo: DELETE_VIDEO
36 +};
37 +
38 +export default routes;
...\ No newline at end of file ...\ No newline at end of file