- mongoDB์ file ์๋ณธ์ ์ ์ฅํ์ง ์๊ณ , file์ location์ ์ ์ฅํ ๊ฒ์ด๋ค.
- Multer๋ผ๋ middleware๋ฅผ ํตํด ์ค๊ฐ์ uploadํ๋ ค๋ ํ์ผ์ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์ฌ ์ ์๋ค.
- Multer์ ํตํด ๋ฐํ๋ฐ์ file์ URL์ mongoDB์ ์ ์ฅํ๋ค.
multer
- multer๋ ํ์ผ ์ ์ฅ์ ์ํด ์ฌ์ฉ๋๋ node.js middleware์ ๋๋ค.
- multer๋ฅผ ์ด์ฉํด์ ํ์ผ์ ์ ์ฅํ๋ฉด ํ์ผ๋ช ์ด ์์๋ก ๋ฐ๋๊ณ ๋ฌธ์ํํ๋ก ๋ณํ๋๊ฒ ๋ฉ๋๋ค.
npm install multer
upload.pug
form(action=`/videos${routes.upload}`, method="post", enctype="multipart/form-data")
label(for="file") Video File
input(type="file", id="file", name="videoFile", required=true, accept="video/*")
input(type="text", name="title", required = true)
textarea(name="description", placeholder="Description", required=true)
input(type="submit", value="Upload now")
- video file๋ง Uploadํ๊ฒ ํ์ผ ํ์ฅ์์ ๋ํ ์ ํ์ ๊ฑด๋ค. input์ accept property๋ฅผ ์ฌ์ฉํ๋ค.
- <input type="file" accept="ํ์ผ ํ์ฅ์|audio/*|video/*|image/*|๋ฏธ๋์ด ํ์
">์ ๊ฐ์ ํ์์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
์ฐ๋ฆฌ๋ video๋ฅผ ์ํ๊ธฐ ๋๋ฌธ์ <input type="file" accept="video/*">๋ผ๊ณ ํ๊ฒ ์ต๋๋ค. - multer๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ form์ ์์ฑ์ธ enctype(์ธ์ฝ๋ฉ ํ์ )์ด multipart/form-data ์ด์ด์ผ ํฉ๋๋ค. enctype์ด ์ง์ ๋์ด ์์ง ์์ผ๋ฉด ์ ์ฅํ ์ ์์ผ๋ ์ ์ํฉ๋๋ค. <form enctype="multipart/form-data">
middlewares.js
import multer from "multer";
const multerVideo = multer({dest:"videos/"});
// 'videos/'๋ก ๋๋ ํ ๋ฆฌ๋ฅผ ์ง์ ํ๋ค. videos/๋ผ๋ ๋ป์ ํ์ฌ ํด๋ ๋ฐ์ videos์ด๋ค.
// '/videos/'๋ก ํ๋ฉด ์ปดํจํฐ ๋ฃจํธํด๋๋ก๋ถํฐ์ ํด๋๊ฒฝ๋ก๋ฅผ ์๋ฏธํ๊ฒ ๋๋ฏ๋ก ์ฃผ์
// ํด๋๋ multer๊ฐ ์๋์ผ๋ก ์์ฑํ๋ฏ๋ก ์ ๊ฒฝ์ฐ์ง ์์๋ ๋๋ค.
export const uploadVideo = multerVideo.single("videoFile")
์ด๋ค ์ด๋ฆ์(form์์ name ์์ฑ) ํ์ผ์ ๋ช๊ฐ ์ ๋ก๋ ํ ์ง๋ ์ ํด์ผํ๋ค.
- ์ด๋ฆ์ form๋ด๋ถ file type input์ name๊ณผ ๋์ผํด์ผ ํฉ๋๋ค.
- single์ ์๋ฏธ๋ ํ๋์ ํ์ผ์ ์ ๋ก๋ํ๋ค๋ ๋ป์ด๋ค.
videoRouter.js
import {uploadVideoMiddleware} from '../middlewares';
videoRouter.post(routes.upload, uploadVideoMiddleware, postUpload);
post method๋ก /upload๋ก ์์ฒญํ๋ฉด ์๋ต์ผ๋ก postUpload๊ฐ ์คํ๋๋๋ฐ์. postUpload๊ฐ ์คํ๋๊ธฐ์ ์ ํ์ผ์ ์ ๋ก๋ํ๊ธฐ์ํด uploadVideoMiddleware๋ฅผ ์ฌ์ด์ ๋ฃ์ด์ค๋๋ค. ๊ทธ๋ฌ๋ฉด ์ด middleware๊ฐ ์ ์ฅํ ํ์ผ์ ๋ํ ์ ๋ณด๋ฅผ req.file์ ๋ฃ์ ํ postUpload๋ฅผ ์คํ์ํต๋๋ค. ์ฐธ๊ณ ๋ก middleware๋ผ๋ฆฌ๋ res,req๋ฅผ ๊ณต์ ํฉ๋๋ค.
videoController
postUpload ํจ์์ ๋ํ ์ฝ๋๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. multer๋ ์ ์ฅํ ํ์ผ์ ๋ํ ์ ๋ณด๋ฅผ req์ file์ ๋ด์์ ๋ณด๋ ๋๋ค. ์๋์ ์ฝ๋๋ก ์ฐ๋ฆฌ๋ file์ ๋ฐ์ ์ ์์ต๋๋ค.
export const postUpload = (req, res) => {
const {file} = req;
console.log(file);
}
upload ํ์ด์ง์์ ์ ๋ก๋๋ฅผ ํ๊ณ ์ฝ์์ ํ์ธํ๋ฉด ์๋์ ๊ฐ์ต๋๋ค.
์
๋ก๋ํ file์ ๋ํ ์ ๋ณด๋ฅผ ๋ณผ ์ ์๋ค์. ์ด ์ค์์ ๋์ฌ๊ฒจ๋ณผ ๊ฒ์ ๋ฐ๋ก path ์
๋๋ค. ์ฐ๋ฆฌ๊ฐ Video Model์ ๋ง๋ค๋ ์ฐ๋ฆฌ๋ database์ file ์์ฒด๋ฅผ ์ ์ฅํ๋ ๊ฒ์ด ์๋๋ผ fileURL์ ์ ์ฅํ ๊ฒ์ด๋ผ๊ณ ํ์์ฃ . ์ด fileURL์ ํด๋นํ๋ ๊ฒ์ด ๋ฐ๋ก path์
๋๋ค. ์ด์ ์ด ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ Video element๋ฅผ ์ถ๊ฐํด๋ณด๊ฒ ์ต๋๋ค.
- Video.create({})
Video model์ ๋ฉ์๋ create์ ์ํ์ฌ element ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ์ฌ ์คํํ๋ฉด ํด๋น element๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๊ณ ์ ์ฅ๋ video ๊ฐ์ฒด๋ฅผ ๋ฐํํด์ค๋๋ค. - newVideo.id
redirectํ๋ ๋ถ๋ถ์ ๋ณด๋ฉด Video model schema๋ก id๋ฅผ ์ ์ด์ค์ ์ ์์ง๋ง mongoDB ์์ฒด์ ์ผ๋ก id๋ฅผ ๋ถ์ฌํ๊ธฐ ๋๋ฌธ์ id ์์ฑ์ ์ฌ์ฉํ ์ ์์ต๋๋ค._id๋ก ํ์ฌ๋ ๋์ผํฉ๋๋ค.
import Video from '../models/Video';
export const postUpload = async(req, res) => {
const {
body : { title, description },
file : { path }
= req;
const newVideo = await Video.create({
fileUrl : path,
title,
description });
res.redirect(routes.videoDetail(newVideo.id));
}
'๐ Back > ๐งฉ Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[npx] npm ๋ช ๋ น์ด๋ ๋ญ๊ฐ ๋ฌ๋ผ? (0) | 2021.03.15 |
---|---|
Aws s3 (0) | 2021.03.09 |
์นํฉ(Webpack) ์ฐ๋ ์ด์ / ์ฌ์ฉ ๋ฐฉ๋ฒ (0) | 2021.02.13 |
์ฌ์ฉ์ ์ธ์ฆ ๋ฏธ๋ค์จ์ด: PassportJS (0) | 2021.02.12 |
mixin: ๊ฐ์ ํ์์ ๋ชจ๋ํํ์ฌ ๋๋๋งํ๊ธฐ (0) | 2021.01.25 |