Flare-k

[Add] multer and Complete Upload video with DB

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