Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김대휘
/
Node.js-Express-Assignment2
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
김대휘
2020-05-02 02:17:31 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
4c63f1407b2ab5aa9c5379ebd69550c18e9f9f7c
4c63f140
0 parents
First commit
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
0 deletions
.gitignore
app.js
package.json
.gitignore
0 → 100644
View file @
4c63f14
node_modules
package-lock.json
yarn.lock
\ No newline at end of file
app.js
0 → 100644
View file @
4c63f14
var
express
=
require
(
'express'
);
var
app
=
express
();
var
bodyParser
=
require
(
'body-parser'
);
var
session
=
require
(
'express-session'
)
app
.
use
(
session
({
secret
:
'keyboard cat'
,
cookie
:
{
maxAge
:
60000
}
}))
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
json
());
var
users
=
new
Array
();
users
[
0
]
=
{
"userId"
:
0
,
"name"
:
"jin"
,
"password"
:
"abc"
,
"isAdmin"
:
true
}
app
.
put
(
'/login'
,
function
(
req
,
res
)
{
// users 배열에서 찾도록 처리 해야 함
// admin 여부를 확인하여 체크
// req.body.id : ID
// req.body.name : 패스워드
const
user
=
req
.
body
;
for
(
var
i
=
0
;
i
<
users
.
length
;
i
++
)
{
if
(
users
[
i
].
userId
==
user
.
userId
&&
users
[
i
].
password
==
user
.
password
)
{
req
.
session
.
userId
=
users
[
i
].
userId
;
req
.
session
.
isAdmin
=
users
[
i
].
isAdmin
;
res
.
send
(
"Login"
);
return
;
}
}
res
.
send
(
"Login Failed"
);
return
;
});
app
.
put
(
'/logout'
,
function
(
req
,
res
)
{
// Logout
// 세션 유효 여부를 체크하고 세션 Delete
if
(
!
users
[
req
.
session
.
userId
])
{
res
.
send
(
"Error!"
);
return
;
}
req
.
session
.
userId
=
null
;
req
.
session
.
isAdmin
=
null
;
res
.
send
(
"LogOut"
);
return
;
});
var
auth
=
function
(
req
,
res
,
next
)
{
// Session Check
// 어드민 여부 체크 필요
console
.
log
(
req
.
session
.
userId
,
req
.
session
.
isAdmin
);
if
(
req
.
session
.
userId
!=
null
&&
req
.
session
.
isAdmin
==
true
)
next
();
else
res
.
send
(
"Not Admin"
);
return
;
};
app
.
get
(
'/users/:userId'
,
auth
,
function
(
req
,
res
)
{
// get User Information
const
user
=
req
.
params
;
for
(
var
i
=
0
;
i
<
users
.
length
;
i
++
)
{
if
(
users
[
i
].
userId
==
user
.
userId
)
{
res
.
send
(
users
[
i
]);
return
;
}
}
res
.
send
(
"Not exist user"
);
return
;
});
app
.
put
(
'/users'
,
auth
,
function
(
req
,
res
)
{
const
user
=
req
.
body
;
for
(
var
i
=
0
;
i
<
users
.
length
;
i
++
)
{
if
(
users
[
i
].
userId
==
user
.
userId
)
{
users
[
i
]
=
{
"userId"
:
user
.
userId
,
"name"
:
user
.
name
,
"password"
:
user
.
password
,
"isAdmin"
:
user
.
isAdmin
}
res
.
send
(
"Updated"
);
return
;
}
}
res
.
send
(
"Not exist user"
);
return
;
});
app
.
post
(
'/users'
,
auth
,
function
(
req
,
res
)
{
user
=
req
.
body
;
users
[
user
.
userId
]
=
{
"userId"
:
user
.
userId
,
"name"
:
user
.
name
,
"password"
:
user
.
password
,
"isAdmin"
:
user
.
isAdmin
}
res
.
send
(
"Added"
);
});
app
.
delete
(
'/users/:userId'
,
auth
,
function
(
req
,
res
)
{
const
user
=
req
.
params
;
for
(
var
i
=
0
;
i
<
users
.
length
;
i
++
)
{
if
(
users
[
i
].
userId
==
user
.
userId
)
{
users
.
splice
(
i
,
1
);
res
.
send
(
"Deleted"
);
return
;
}
}
res
.
send
(
"Not exist user"
);
return
;
});
// 사용자 추가 시에 admin 여부도 추가해야 함
var
server
=
app
.
listen
(
80
);
\ No newline at end of file
package.json
0 → 100644
View file @
4c63f14
{
"name"
:
"node.js-express-assignment2"
,
"version"
:
"1.0.0"
,
"description"
:
""
,
"main"
:
"index.js"
,
"dependencies"
:
{
"express"
:
"^4.17.1"
,
"express-session"
:
"^1.17.1"
},
"devDependencies"
:
{},
"scripts"
:
{
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
},
"author"
:
""
,
"license"
:
"ISC"
}
Please
register
or
login
to post a comment