Toggle navigation
Toggle navigation
This project
Loading...
Sign in
강연욱
/
myYoutube
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
2
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Flare-k
2020-05-26 22:03:26 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
85585a5553a19615e85896f39eeff6a25f340dc2
85585a55
1 parent
127cd672
[Add] multer and Complete Upload video with DB
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
27 additions
and
10 deletions
controllers/videoController.js
middlewares.js
package.json
routers/videoRouter.js
views/upload.pug
controllers/videoController.js
View file @
85585a5
...
...
@@ -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
)
=>
...
...
middlewares.js
View file @
85585a5
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
...
...
package.json
View file @
85585a5
...
...
@@ -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"
:
{
...
...
routers/videoRouter.js
View file @
85585a5
...
...
@@ -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
);
...
...
views/upload.pug
View file @
85585a5
...
...
@@ -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
...
...
Please
register
or
login
to post a comment