-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
222 lines (197 loc) · 5.65 KB
/
main.js
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
const express = require('express');
const path = require('path');
const cors = require("cors")
const app = express();
const bodyParser = require("body-parser");
const maria = require('./maria');
const bcrypt = require("bcrypt");
const cookieParser = require('cookie-parser');
const session = require("express-session")
const nunjucks = require('nunjucks');
const morgan = require('morgan');
const saltRounds = 10;
maria.connect();
const PORT = process.env.PORT || 30000
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(cors());
app.use(cookieParser());
app.set('view engine', 'html');
nunjucks.configure('public/layout', {
express: app,
watch: true,
});
app.use(
session({
key: "userId",
secret: "rorkxdmsrj",
resave: false,
saveUninitialized: false,
cookie: {
expires: 60 * 60 * 24,
},
})
);
app.use(
cors({
origin: [`http://localhost:${PORT}`],
method: ["GET", "POST"],
credentials: true,
})
);
app.use('/static', express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
if (req.session.user) {
console.log(req.session.user.id)
const userInfo = req.session.user.id;
res.render('index.html', {
isLogin: true,
userInfo,
});
} else {
res.render('index.html', { isLogin: false });
}
});
app.post('/blog/getBlogList', (req,res) => {
var query_txt = "SELECT * FROM Board ORDER BY BoardID DESC LIMIT 5"
maria.query(query_txt, function(err, rows, fields) {
if(!err) {
console.log(rows)
res.send(rows); // responses send rows
} else {
console.log("err : " + err);
res.send(err); // response send err
}
});
})
app.post('/blog/getBlogDetail', (req,res) => {
console.log(req.body)
var query_txt = "SELECT * FROM Board WHERE Writter =" + req.body["id"]
maria.query(query_txt, function(err, rows, fields) {
if(!err) {
res.send(rows); // responses send rows
} else {
console.log("err : " + err);
res.send(err); // response send err
}
});
})
function getRegDate(){
var today = new Date();
var year = today.getFullYear();
var month = ('0' + (today.getMonth() + 1)).slice(-2);
var day = ('0' + today.getDate()).slice(-2);
var hours = ('0' + today.getHours()).slice(-2);
var minutes = ('0' + today.getMinutes()).slice(-2);
var seconds = ('0' + today.getSeconds()).slice(-2);
var dateString = year + '-' + month + '-' + day;
var timeString = hours + ':' + minutes + ':' + seconds;
var regdate = dateString + " " +timeString;
return regdate;
}
app.post('api/blog/addNewBoarder', (req,res) => {
console.log(req.body)
var query_txt = "INSERT INTO Board (Writter, contents, BoardTitle, regdate) VALUE ('" + req.body["writter"] + "','" + req.body['content'] + "','" + req.body['title'] + "','" +getRegDate() +"')";
maria.query(query_txt, function(err, rows, fields) {
if(!err) {
console.log("aaa");
res.send(true);
} else {
console.log("err : " + err);
res.send(err); // response send err
}
});
});
app.post("/api/auth/register", (req, res) => {
bcrypt.hash(req.body.password, saltRounds, (err, hash) => {
if (err) {
console.log(err);
} else {
maria.query(
`INSERT INTO user (id, password) VALUES (?, ?)`,
[req.body.username, hash],
(error, results, fields) => {
if (error) {
console.log(error);
res.send({
status: 400,
});
} else {
res.send("<script>alert('회원가입되었습니다.');location.href='/static/layout/login.html';</script>");
// res.send({
// status: 200,
// results: results,
// });
}
}
);
}
});
});
app.post("/api/auth/login", (req, res) => {
maria.query(
`SELECT * FROM user WHERE id = ?`,
[req.body.username],
(error, results, fields) => {
if (error) {
res.send({
status: "error",
});
} else {
if (results.length > 0) {
bcrypt.compare(
req.body.password,
results[0].password,
(err, response) => {
req.session.user = results[0];
// console.log(req.session);
if (response) {
res.redirect('/?msg=로그인 되었습니다.');
} else {
res.send({
status: "incorrect password",
});
}
}
);
} else {
console.log("no user");
res.send({
status: "error",
});
}
}
}
);
});
//리펙토링 방법 session과 로딩할 블로그를 보내서 처리하면됨
app.get(("/api/render/blog"), (req,res) => {
if (req.session.user) {
console.log(req.session.user.id)
const userInfo = req.session.user.id;
res.render('blog/blog.html', {
isLogin: true,
userInfo,
});
} else {
res.render('blog/blog.html', { isLogin: false });
}
});
app.get(("/api/render/blogWrite"), (req,res) =>{
if (req.session.user) {
console.log(req.session.user.id)
const userInfo = req.session.user.id;
res.render('blog/sub/boardWrite.html', {
isLogin: true,
userInfo,
});
} else {
res.send(`<script type="text/javascript">alert("로그인 후 이용해주세요"); window.location = document.referrer; </script>`);
}
// res.render('blog/sub/boardWrite.html');
});
app.listen(3000, () => console.log(`Example app listening on port 3000!`));