Flare-k

[Add] axios and ajax

...@@ -13,6 +13,7 @@ import routes from "./routes"; ...@@ -13,6 +13,7 @@ import routes from "./routes";
13 import userRouter from "./routers/userRouter"; 13 import userRouter from "./routers/userRouter";
14 import videoRouter from "./routers/videoRouter"; 14 import videoRouter from "./routers/videoRouter";
15 import globalRouter from "./routers/globalRouter"; 15 import globalRouter from "./routers/globalRouter";
16 +import aipRouter from "./routers/apiRouter";
16 import "./passport"; 17 import "./passport";
17 18
18 dotenv.config(); 19 dotenv.config();
...@@ -45,5 +46,5 @@ app.use(localsMiddleware); ...@@ -45,5 +46,5 @@ app.use(localsMiddleware);
45 app.use(routes.home, globalRouter); 46 app.use(routes.home, globalRouter);
46 app.use(routes.users, userRouter); 47 app.use(routes.users, userRouter);
47 app.use(routes.videos, videoRouter); 48 app.use(routes.videos, videoRouter);
48 - 49 +app.use(routes.api, aipRouter);
49 export default app; // 파일을 불러올때 app object를 준다는 의미. 50 export default app; // 파일을 불러올때 app object를 준다는 의미.
......
...@@ -7,6 +7,12 @@ const currentTime = document.getElementById("currentTime"); ...@@ -7,6 +7,12 @@ const currentTime = document.getElementById("currentTime");
7 const totalTime = document.getElementById("totalTime"); 7 const totalTime = document.getElementById("totalTime");
8 const volumeRange = document.getElementById("jsVolume"); 8 const volumeRange = document.getElementById("jsVolume");
9 9
10 +const registerView = () => {
11 + fetch(`/api/${window.location.href.split("/videos/")[1]}/view`, {
12 + method: "POST",
13 + });
14 +};
15 +
10 // video element MDN 16 // video element MDN
11 // using MDN (Mozila Developer Networks) 17 // using MDN (Mozila Developer Networks)
12 function handlePlayClick() { 18 function handlePlayClick() {
...@@ -90,6 +96,7 @@ function setTotalTime() { ...@@ -90,6 +96,7 @@ function setTotalTime() {
90 96
91 // 영상이 끝나면 pause하고 다시 앞으로 돌릴 수 있게 하고 싶다... 97 // 영상이 끝나면 pause하고 다시 앞으로 돌릴 수 있게 하고 싶다...
92 function handleEnded() { 98 function handleEnded() {
99 + registerView();
93 videoPlayer.currentTime = 0; 100 videoPlayer.currentTime = 0;
94 playBtn.innerHTML = '<i class="fas fa-play"></i>'; 101 playBtn.innerHTML = '<i class="fas fa-play"></i>';
95 } 102 }
......
...@@ -119,3 +119,20 @@ export const deleteVideo = async (req, res) => { ...@@ -119,3 +119,20 @@ export const deleteVideo = async (req, res) => {
119 // 삭제를 실패하던 성공하던 home으로 redirect한다. 119 // 삭제를 실패하던 성공하던 home으로 redirect한다.
120 res.redirect(routes.home); 120 res.redirect(routes.home);
121 }; 121 };
122 +
123 +// 조회수 부분
124 +export const postRegisterView = async (req, res) => {
125 + const {
126 + params: { id },
127 + } = req;
128 + try {
129 + const video = await Video.findById(id);
130 + video.views += 1;
131 + video.save();
132 + res.status(200);
133 + } catch (error) {
134 + res.status(400);
135 + } finally {
136 + res.end();
137 + }
138 +};
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
21 "@babel/polyfill": "^7.10.1", 21 "@babel/polyfill": "^7.10.1",
22 "@babel/preset-env": "^7.9.6", 22 "@babel/preset-env": "^7.9.6",
23 "autoprefixer": "^9.8.0", 23 "autoprefixer": "^9.8.0",
24 + "axios": "^0.19.2",
24 "babel-loader": "^8.1.0", 25 "babel-loader": "^8.1.0",
25 "body-parser": "^1.19.0", 26 "body-parser": "^1.19.0",
26 "connect-mongo": "^3.2.0", 27 "connect-mongo": "^3.2.0",
......
1 +import express from "express";
2 +import routes from "../routes";
3 +import { postRegisterView } from "../controllers/videoController";
4 +
5 +const apiRouter = express.Router();
6 +
7 +apiRouter.post(routes.registerView, postRegisterView);
8 +
9 +export default apiRouter;
...@@ -27,6 +27,10 @@ const GITHUB_CALLBACK = "/auth/github/callback"; ...@@ -27,6 +27,10 @@ const GITHUB_CALLBACK = "/auth/github/callback";
27 const FB = "/auth/facebook"; 27 const FB = "/auth/facebook";
28 const FB_CALLBACK = "/auth/facebook/callback"; 28 const FB_CALLBACK = "/auth/facebook/callback";
29 29
30 +// API
31 +const API = "/api";
32 +const REGISTER_VIEW = "/:id/view";
33 +
30 const routes = { 34 const routes = {
31 home: HOME, 35 home: HOME,
32 join: JOIN, 36 join: JOIN,
...@@ -71,6 +75,9 @@ const routes = { ...@@ -71,6 +75,9 @@ const routes = {
71 me: ME, 75 me: ME,
72 facebook: FB, 76 facebook: FB,
73 facebookCallback: FB_CALLBACK, 77 facebookCallback: FB_CALLBACK,
78 +
79 + api: API,
80 + registerView: REGISTER_VIEW,
74 }; 81 };
75 // template에서 직접 접근이 필요한 경우 함수로 바꿔준다. 82 // template에서 직접 접근이 필요한 경우 함수로 바꿔준다.
76 export default routes; 83 export default routes;
......