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-06-15 19:44:55 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e8633805b9a3f9bbdd377ac9ffd4301df7e44b50
e8633805
1 parent
1f3f6a50
session
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
80 additions
and
39 deletions
app.js
controllers/userController.js
middlewares.js
package.json
routers/globalRouter.js
routers/userRouter.js
routers/videoRouter.js
routes.js
text/study.txt
views/partials/header.pug
app.js
View file @
e863380
...
...
@@ -3,14 +3,22 @@ import morgan from "morgan";
import
helmet
from
"helmet"
;
import
cookieParser
from
"cookie-parser"
;
import
bodyParser
from
"body-parser"
;
import
passport
from
"passport"
;
import
mongoose
from
"mongoose"
;
import
session
from
"express-session"
;
import
MongoStore
from
"connect-mongo"
;
import
{
localsMiddleware
}
from
"./middlewares"
;
import
routes
from
"./routes"
;
import
userRouter
from
"./routers/userRouter"
;
import
videoRouter
from
"./routers/videoRouter"
;
import
globalRouter
from
"./routers/globalRouter"
;
import
"./passport"
;
const
app
=
express
();
const
CokieStore
=
MongoStore
(
session
);
app
.
use
(
helmet
());
app
.
set
(
"view engine"
,
"pug"
);
app
.
use
(
"/uploads"
,
express
.
static
(
"uploads"
));
...
...
@@ -20,6 +28,17 @@ app.use(bodyParser.json());
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
true
}));
// json, html, text, urlencoded 할 거 없이 다 parser할 수 있도록 설정해줘야 한다.
app
.
use
(
morgan
(
"dev"
));
app
.
use
(
session
({
secret
:
process
.
env
.
COOKIE_SECRET
,
resave
:
true
,
saveUninitialized
:
false
,
store
:
new
CokieStore
({
mongooseConnection
:
mongoose
.
connection
}),
})
);
app
.
use
(
passport
.
initialize
());
app
.
use
(
passport
.
session
());
app
.
use
(
localsMiddleware
);
app
.
use
(
routes
.
home
,
globalRouter
);
app
.
use
(
routes
.
users
,
userRouter
);
...
...
controllers/userController.js
View file @
e863380
import
passport
from
"passport"
;
import
routes
from
"../routes"
;
import
User
from
"../models/User"
;
...
...
@@ -5,7 +6,7 @@ import User from "../models/User";
export
const
getJoin
=
(
req
,
res
)
=>
{
res
.
render
(
"join"
,
{
pageTitle
:
"Join"
});
};
export
const
postJoin
=
async
(
req
,
res
)
=>
{
export
const
postJoin
=
async
(
req
,
res
,
next
)
=>
{
const
{
body
:
{
name
,
email
,
password
,
password2
},
}
=
req
;
...
...
@@ -19,20 +20,23 @@ export const postJoin = async (req, res) => {
email
,
});
await
User
.
register
(
user
,
password
);
next
();
}
catch
(
error
)
{
// eslint-disable-next-line no-console
console
.
log
(
error
);
res
.
redirect
(
routes
.
home
);
}
// To Do: Register User
// To Do: Log user in
res
.
redirect
(
routes
.
home
);
}
};
export
const
getLogin
=
(
req
,
res
)
=>
res
.
render
(
"login"
,
{
pageTitle
:
"Login"
});
export
const
postLogin
=
(
req
,
res
)
=>
{
res
.
redirect
(
routes
.
home
);
};
export
const
postLogin
=
passport
.
authenticate
(
"local"
,
{
failureRedirect
:
routes
.
login
,
successRedirect
:
routes
.
home
,
});
// 로그아웃을 클릭하면 LogOut페이지로 가는 것 대신에, 로그아웃을 처리한 후
// home 페이지로 Redirect로 표현할 것이다.
...
...
@@ -42,7 +46,7 @@ export const logout = (req, res) => {
res
.
redirect
(
routes
.
home
);
};
export
const
users
=
(
req
,
res
)
=>
res
.
render
(
"users"
,
{
pageTitle
:
"Users"
});
//
export const users = (req, res) => res.render("users", { pageTitle: "Users" });
export
const
userDetail
=
(
req
,
res
)
=>
res
.
render
(
"userDetail"
,
{
pageTitle
:
"User Detail"
});
export
const
editProfile
=
(
req
,
res
)
=>
...
...
middlewares.js
View file @
e863380
...
...
@@ -6,12 +6,23 @@ const multerVideo = multer({ dest: "uploads/videos/" });
export
const
localsMiddleware
=
(
req
,
res
,
next
)
=>
{
res
.
locals
.
siteName
=
"my Youtube"
;
res
.
locals
.
routes
=
routes
;
res
.
locals
.
user
=
{
isAuthenticated
:
false
,
id
:
1
,
};
res
.
locals
.
user
=
req
.
user
||
null
;
next
();
};
export
const
onlyPublic
=
(
req
,
res
,
next
)
=>
{
if
(
req
.
user
)
{
res
.
redirect
(
routes
.
home
);
}
else
{
next
();
}
};
export
const
onlyPrivate
=
(
req
,
res
,
next
)
=>
{
if
(
req
.
user
)
{
next
();
}
else
{
res
.
redirect
(
routes
.
home
);
}
};
export
const
uploadVideo
=
multerVideo
.
single
(
"videoFile"
);
// single에 들어간 videoFile은 upload.pug의 file 부분 input name
...
...
package.json
View file @
e863380
...
...
@@ -22,10 +22,12 @@
"autoprefixer"
:
"^9.8.0"
,
"babel-loader"
:
"^8.1.0"
,
"body-parser"
:
"^1.19.0"
,
"connect-mongo"
:
"^3.2.0"
,
"cookie-parser"
:
"^1.4.5"
,
"css-loader"
:
"^3.5.3"
,
"dotenv"
:
"^8.2.0"
,
"express"
:
"^4.17.1"
,
"express-session"
:
"^1.17.1"
,
"extract-text-webpack-plugin"
:
"^4.0.0-beta.0"
,
"helmet"
:
"^3.22.0"
,
"mongoose"
:
"^5.9.15"
,
...
...
routers/globalRouter.js
View file @
e863380
...
...
@@ -2,23 +2,23 @@ import express from "express";
import
routes
from
"../routes"
;
import
{
home
,
search
}
from
"../controllers/videoController"
;
import
{
logout
,
getJoin
,
postJoin
,
getLogin
,
postLogin
,
logout
,
getJoin
,
postJoin
,
getLogin
,
postLogin
,
}
from
"../controllers/userController"
;
import
{
onlyPublic
}
from
"../middlewares"
;
const
globalRouter
=
express
.
Router
();
globalRouter
.
get
(
routes
.
home
,
home
);
globalRouter
.
get
(
routes
.
join
,
getJoin
);
globalRouter
.
post
(
routes
.
join
,
postJoin
);
globalRouter
.
get
(
routes
.
join
,
onlyPublic
,
getJoin
);
globalRouter
.
post
(
routes
.
join
,
onlyPublic
,
postJoin
,
postLogin
);
globalRouter
.
get
(
routes
.
login
,
getLogin
);
globalRouter
.
post
(
routes
.
login
,
postLogin
);
globalRouter
.
get
(
routes
.
login
,
onlyPublic
,
getLogin
);
globalRouter
.
post
(
routes
.
login
,
onlyPublic
,
postLogin
);
globalRouter
.
get
(
routes
.
logout
,
logout
);
globalRouter
.
get
(
routes
.
home
,
home
);
globalRouter
.
get
(
routes
.
search
,
search
);
export
default
globalRouter
;
\ No newline at end of file
globalRouter
.
get
(
routes
.
logout
,
logout
);
export
default
globalRouter
;
...
...
routers/userRouter.js
View file @
e863380
import
express
from
"express"
;
import
routes
from
"../routes"
;
import
{
userDetail
,
editProfile
,
changePassword
,
userDetail
,
editProfile
,
changePassword
,
}
from
"../controllers/userController"
;
import
{
onlyPrivate
}
from
"../middlewares"
;
const
userRouter
=
express
.
Router
();
userRouter
.
get
(
routes
.
editProfile
,
editProfile
);
userRouter
.
get
(
routes
.
changePassword
,
changePassword
);
userRouter
.
get
(
routes
.
editProfile
,
onlyPrivate
,
editProfile
);
userRouter
.
get
(
routes
.
changePassword
,
onlyPrivate
,
changePassword
);
userRouter
.
get
(
routes
.
userDetail
(),
userDetail
);
export
default
userRouter
;
/*
userRouter.get("/", (req, res) => res.send("user index"));
userRouter.get("/edit", (req, res) => res.send("user edit"));
...
...
@@ -25,4 +25,4 @@ app.use("/user", userRouter)
이런식으로 사용하면 app.js에서 하나하나 라우터를 만드는 방법과 달리
/user라 필요한 라우터에 대한 라우터들을 모두 import할 수 있게 된다..
(ex, /user, /user/edit, /user/password ...)
*/
\ No newline at end of file
*/
...
...
routers/videoRouter.js
View file @
e863380
...
...
@@ -8,22 +8,22 @@ import {
postEditVideo
,
deleteVideo
,
}
from
"../controllers/videoController"
;
import
{
uploadVideo
}
from
"../middlewares"
;
import
{
uploadVideo
,
onlyPrivate
}
from
"../middlewares"
;
// export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다.
const
videoRouter
=
express
.
Router
();
// Upload
videoRouter
.
get
(
routes
.
upload
,
getUpload
);
videoRouter
.
post
(
routes
.
upload
,
uploadVideo
,
postUpload
);
videoRouter
.
get
(
routes
.
upload
,
onlyPrivate
,
getUpload
);
videoRouter
.
post
(
routes
.
upload
,
onlyPrivate
,
uploadVideo
,
postUpload
);
// Video Detail
videoRouter
.
get
(
routes
.
videoDetail
(),
videoDetail
);
// Video Edit
videoRouter
.
get
(
routes
.
editVideo
(),
getEditVideo
);
videoRouter
.
post
(
routes
.
editVideo
(),
postEditVideo
);
videoRouter
.
get
(
routes
.
editVideo
(),
onlyPrivate
,
getEditVideo
);
videoRouter
.
post
(
routes
.
editVideo
(),
onlyPrivate
,
postEditVideo
);
// Video Delete
videoRouter
.
get
(
routes
.
deleteVideo
(),
deleteVideo
);
videoRouter
.
get
(
routes
.
deleteVideo
(),
onlyPrivate
,
deleteVideo
);
export
default
videoRouter
;
...
...
routes.js
View file @
e863380
...
...
@@ -58,5 +58,5 @@ const routes = {
}
},
};
// template에서 직접 접근이 필요한 경우 함수로 바꿔준다.
export
default
routes
;
...
...
text/study.txt
0 → 100644
View file @
e863380
express session을 설치한다. npm install express-session
postJoin은 이메일과 비밀번호를 전달하고 next()가 호출되어 postLogin으로 간다.
connect mongo를 통해 저장소를 생성한다.
\ No newline at end of file
views/partials/header.pug
View file @
e863380
...
...
@@ -8,7 +8,7 @@ header.header
input(type="text", placeholder="Search by term...", name="term")
.header__column
ul
if !user
.isAuthenticated
if !user
li
a(href=routes.join) Join
li
...
...
Please
register
or
login
to post a comment