app.js
2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);