Flare-k

[Add] multer and Complete Upload video with DB

...@@ -25,13 +25,23 @@ export const search = (req, res) => { ...@@ -25,13 +25,23 @@ export const search = (req, res) => {
25 //upload 또한 upload를 준비하기 위한 get 페이지와 실제 데이터를 보내는 post 페이지가 필요하다. 25 //upload 또한 upload를 준비하기 위한 get 페이지와 실제 데이터를 보내는 post 페이지가 필요하다.
26 export const getUpload = (req, res) => 26 export const getUpload = (req, res) =>
27 res.render("upload", { pageTitle: "Upload" }); 27 res.render("upload", { pageTitle: "Upload" });
28 -export const postUpload = (req, res) => { 28 +export const postUpload = async(req, res) => {
29 //const {} 를 통해 body를 받아와 요청하는 정보들을 확인한다. 29 //const {} 를 통해 body를 받아와 요청하는 정보들을 확인한다.
30 //이는 pug와 db.js를 확인해야하는 듯 하다. 30 //이는 pug와 db.js를 확인해야하는 듯 하다.
31 const { 31 const {
32 - body: { file, title, description }, 32 + body: { title, description },
33 - } = req; 33 + file: { path },
34 - res.redirect(routes.videoDetail(324393)); 34 + } = req; //file에 path라는 요소가 있다.
35 +
36 + const newVideo = await Video.create({
37 + fileUrl: path,
38 + title,
39 + description,
40 + //여기있는 fileUrl, title, description은 videoDB의 속성이다.
41 + });
42 + console.log(newVideo);
43 + res.redirect(routes.videoDetail(newVideo.id)); //id는 DB의 id
44 + //id는 mongoDB가 랜덤하게 만들어준다.
35 }; 45 };
36 46
37 export const videoDetail = (req, res) => 47 export const videoDetail = (req, res) =>
......
1 +import multer from "multer";
1 import routes from "./routes"; 2 import routes from "./routes";
2 3
4 +const multerVideo = multer({ dest: "videos/" });
5 +
3 export const localsMiddleware = (req, res, next) => { 6 export const localsMiddleware = (req, res, next) => {
4 - res.locals.siteName = 'my Youtube'; 7 + res.locals.siteName = "my Youtube";
5 res.locals.routes = routes; 8 res.locals.routes = routes;
6 res.locals.user = { 9 res.locals.user = {
7 isAuthenticated: true, 10 isAuthenticated: true,
...@@ -9,3 +12,6 @@ export const localsMiddleware = (req, res, next) => { ...@@ -9,3 +12,6 @@ export const localsMiddleware = (req, res, next) => {
9 }; 12 };
10 next(); 13 next();
11 }; 14 };
15 +
16 +export const uploadVideo = multerVideo.single("videoFile");
17 +//single에 들어간 videoFile은 upload.pug의 file 부분 input name
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
23 "helmet": "^3.22.0", 23 "helmet": "^3.22.0",
24 "mongoose": "^5.9.15", 24 "mongoose": "^5.9.15",
25 "morgan": "^1.10.0", 25 "morgan": "^1.10.0",
26 + "multer": "^1.4.2",
26 "pug": "^2.0.4" 27 "pug": "^2.0.4"
27 }, 28 },
28 "devDependencies": { 29 "devDependencies": {
......
...@@ -7,12 +7,12 @@ import { ...@@ -7,12 +7,12 @@ import {
7 editVideo, 7 editVideo,
8 deleteVideo, 8 deleteVideo,
9 } from "../controllers/videoController"; 9 } from "../controllers/videoController";
10 +import { uploadVideo } from "../middlewares";
10 //export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다. 11 //export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다.
11 const videoRouter = express.Router(); 12 const videoRouter = express.Router();
12 13
13 videoRouter.get(routes.upload, getUpload); 14 videoRouter.get(routes.upload, getUpload);
14 -videoRouter.post(routes.upload, postUpload); 15 +videoRouter.post(routes.upload, uploadVideo, postUpload);
15 -
16 16
17 videoRouter.get(routes.editVideo, editVideo); 17 videoRouter.get(routes.editVideo, editVideo);
18 18
......
...@@ -2,9 +2,9 @@ extends layouts/main ...@@ -2,9 +2,9 @@ extends layouts/main
2 2
3 block content 3 block content
4 .form-container 4 .form-container
5 - form(action=`/videos${routes.upload}`, method="post") 5 + form(action=`/videos${routes.upload}`, method="post", enctype="multipart/form-data")
6 label(for="file") Video File 6 label(for="file") Video File
7 - input(type="file", id="file", name="file", required=true) 7 + input(type="file", id="file", name="videoFile", required=true, accept = "video/*")
8 input(type="text", placeholder="Title", name="title", required=true) 8 input(type="text", placeholder="Title", name="title", required=true)
9 textarea(name="description", placeholder="Description", required=true) 9 textarea(name="description", placeholder="Description", required=true)
10 input(type="submit", value="Upload Video") 10 input(type="submit", value="Upload Video")
...\ No newline at end of file ...\ No newline at end of file
......