server.js
4.36 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const express = require("express");
const path = require("path");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const dotenv = require("dotenv");
const bodyParser = require("body-parser");
dotenv.config();
const indexRouter = require('./routes/index.js');
const loginRouter = require('./routes/login.js');
const registerRouter = require('./routes/register.js');
const app = express();
app.set('port', process.env.PORT || 3001);
const port = app.get('port');
//미들웨어 설정
app.use(morgan('dev'));
app.use(express.json()); //json형식으로 데이터 전달
app.use(express.urlencoded({extende: false})); // url형식으로 형식으로 데이터 전달
//app.use(bodyParser().json);
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(session({
resave: false,
saveUninitialized: false,
secret: 'keyboard cat' ,//process.env.COOKIE_SECRET,
cookie: {
httpOnly: true,
secure:false,
},
name: 'session-cookie',
}));
//라우팅
//app.use('/',indexRouter);
//app.use('/login',loginRouter);
//app.use('/',loginRouter); //로그인 페이지
app.use('/api/register',registerRouter); // 회원가입 페이지
//에러처리 미들웨어
//존재하지 않는 경로가 들어오면 오류 응답
app.use((req,res,next)=>{
console.log("유저가 존재하지 않은 경로 접근");
res.status(404).send("NOT FOUND");
});
//포트를 연다.
app.listen(port, ()=>{
console.log(port,"번 포트로 대기중");
});
//라우팅
/*
//루트페이지
app.get('/', (req,res)=>{
res.sendFile(path.join(__dirname, '/fe/index.html'));
});
//로그인페이지
app.get('/login', (req,res)=>{
res.send("로그인페이지");
});
app.get('/register', (req,res)=>{
res.send("회원가입 페이지");
});
*/
/* 물어볼 부분
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" : 1,
"name" : "Oh",
"password" : "abc",
"isAdmin" : true
}
users[1] = {
"userId" : 1,
"name" : "Jung",
"password" : "abc",
"isAdmin" : true
}
users[2] = {
"userId" : 2,
"name" : "Go",
"password" : "abc",
"isAdmin" : true
}
app.put('/login', function (req, res) {
var count = 0;
var Found = false;
while(count < users.length) {
if (req.body.userId == users[count].userId
&& req.body.password == users[count].password) {
req.session.userId = users[count].userId;
req.session.isAdmin = users[count].isAdmin;
Found = true;
break;
}
else {
count++;
}
}
if(Found) {
res.send("Login");
}
else {
res.send("Failed");
}
});
app.put('/logout', function (req, res) {
if (req.session.userId == req.body.userId) {
req.session.userId = null;
req.session.isAdmin = false;
res.send("LogOut");
}
else {
res.send("Failed");
}
});
var auth = function (req, res, next) {
if (req.session.userId != null
&& req.session.isAdmin == true)
next();
else
res.send("Error");
};
app.get('/users/:userId', auth,function (req, res) {
var inputId = req.params.userId;
var count = 0;
var Found = false;
while(count < users.length) {
if(inputId == users[count].userId){
Found = true;
res.send(users[count]);
break;
}
else {
count++;
}
}
if (!Found) {
res.send("Not Found");
}
});
app.put('/users',auth,function(req, res){
var inputId = req.body.userId;
var count = 0;
var Found = false;
while (count < users.length) {
if (inputId == users[count].userId) {
Found = true;
users[count] = req.body;
res.send(users[count]);
break;
}
else {
count++;
}
}
if(!Found) {
res.send("Not Found");
}
});
app.post('/users',auth,function(req, res){
var inputData = {
"userId" : req.body.userId,
"name" : req.body.name,
"password" : req.body.password,
"isAdmin" : req.body.isAdmin
}
users.push(inputData);
res.send(inputData);
});
app.delete('/users/:userId',auth,function(req, res){
var inputId = req.params.userId;
var count = 0;
var Found = false;
var removed;
while (count < users.length) {
if (inputId == users[count].userId) {
Found = true;
removed = users.splice(count,1);
console.log(users.length);
res.send(removed);
break;
}
else {
count++;
}
}
if (!Found) {
res.send("Not Found");
}
});
*/
//var server = app.listen(80);