Toggle navigation
Toggle navigation
This project
Loading...
Sign in
오석진
/
sdfsdf
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
오석진
2022-05-09 22:38:45 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
56ed8d8a8f3635cc026406c154468781cc62d19d
56ed8d8a
0 parents
transport
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
114 additions
and
0 deletions
app.js
package.json
app.js
0 → 100644
View file @
56ed8d8
let
express
=
require
(
'express'
);
let
app
=
express
();
let
bodyParser
=
require
(
'body-parser'
);
let
session
=
require
(
'express-session'
)
app
.
use
(
session
({
secret
:
'keyboard cat'
,
cookie
:
{
maxAge
:
60000
}}))
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
json
());
let
users
=
new
Array
();
users
[
0
]
=
{
"userId"
:
0
,
"name"
:
"jin"
,
"password"
:
"abc"
,
"isAdmin"
:
true
}
app
.
put
(
'/login'
,
(
req
,
res
)
=>
{
if
(
req
.
body
.
userId
==
users
[
req
.
body
.
userId
].
userId
&&
req
.
body
.
password
==
users
[
req
.
body
.
userId
].
password
){
req
.
session
.
userId
=
users
[
req
.
body
.
userId
].
isAdmin
res
.
send
(
"Login"
);
}
else
res
.
send
(
"invalid id"
)
// users 배열에서 찾도록 처리 해야 함
// admin 여부를 확인하여 체크
// req.body.id : ID
// req.body.password : 패스워드
});
app
.
put
(
'/logout'
,
(
req
,
res
)
=>
{
// Logout
// 세션 유효 여부를 체크하고 세션 Delete
req
.
session
.
userId
=
null
;
res
.
send
(
"LogOut"
);
});
let
auth
=
(
req
,
res
,
next
)
=>
{
// Session Check
// 어드민 여부 체크 필요
if
(
req
.
session
.
userId
!=
null
&&
req
.
session
.
isAdmin
==
true
)
next
();
else
res
.
send
(
"Error"
);
};
app
.
get
(
'/user/:userId'
,
auth
,
(
req
,
res
)
=>
{
const
userId
=
req
.
params
.
userId
console
.
log
(
users
[
userId
]);
res
.
send
(
users
[
userId
]);
// get User Information
// res.send("OK");
req
.
session
.
userId
=
true
;
});
app
.
post
(
'/user/:userId'
,
auth
,
(
req
,
res
)
=>
{
const
userId
=
req
.
params
.
userId
users
[
userId
]
=
{
"userId"
:
req
.
body
.
userId
,
"name"
:
req
.
body
.
name
,
"password"
:
req
.
body
.
password
,
"isAdmin"
:
req
.
body
.
isAdmin
}
// users[userId]=[req.body.userId, req.body.name, req.body.password, req.body.isAdmin];
// get User Information
req
.
session
.
userId
=
true
;
res
.
send
(
"OK"
);
});
app
.
put
(
'/user/:userId'
,
auth
,
(
req
,
res
)
=>
{
const
userId
=
req
.
params
.
userId
;
users
[
userId
]
=
{
"userId"
:
req
.
body
.
userId
,
"name"
:
req
.
body
.
name
,
"password"
:
req
.
body
.
password
,
"isAdmin"
:
req
.
body
.
isAdmin
}
// users[userId]=[req.body.userId, req.body.name, req.body.password, req.body.isAdmin];
// get User Information
req
.
session
.
userId
=
true
;
res
.
send
(
"OK"
);
});
app
.
delete
(
'/user/:userId'
,
auth
,
(
req
,
res
)
=>
{
const
userId
=
req
.
params
.
userId
;
delete
users
[
userId
];
console
.
log
(
users
);
req
.
session
.
userId
=
true
;
// get User Information
res
.
send
(
"OK"
);
});
// 사용자 추가 시에 admin 여부도 추가해야 함
let
server
=
app
.
listen
(
80
);
\ No newline at end of file
package.json
0 → 100644
View file @
56ed8d8
{
"name"
:
"assignment02"
,
"version"
:
"1.0.0"
,
"description"
:
""
,
"main"
:
"index.js"
,
"scripts"
:
{
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
},
"author"
:
""
,
"license"
:
"ISC"
,
"dependencies"
:
{
"body-parser"
:
"^1.17.1"
,
"express"
:
"^4.18.1"
,
"express-session"
:
"^1.15.2"
}
}
Please
register
or
login
to post a comment