Flare-k

MVC Patterns

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