-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
260 lines (254 loc) · 7.9 KB
/
server.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const { User, Comments } = require('./models')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const jwt = require('jsonwebtoken')
const SECRET = 'asjoiesdffh123wouaeniv124j91'
app.use(express.json())
app.use(require('cors')())
app.use(express.static(__dirname + '/public'))
// app.use(express.static('public/css'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.set('view')
// 注册
app.post('/api/register', async (req, res) => {
const data = req.body
if (!data.username.length) {
return res.send('<h1>the username is empty!</h1><a href=" / ">返回</a>')
}
if (data.password.length < 6) {
return res.send('<h1>the password must be longer than 6!</h1><a href="/">返回</a>')
}
const user = await User.findOne({
username: data.username
})
if (!user) {
const user = await User.create({
username: data.username,
password: data.password,
level: 0
});
const token = jwt.sign({
id: String(user._id),
}, SECRET)
const url = 'http://localhost:8080/comments/' + '?username=' + user.username + '&token=' + token
res.redirect(url)
}
else {
return res.send('<h1>the username has existed!</h1> <a href=" / ">返回</a>')
}
})
// 登录
app.post('/api/login', async (req, res) => {
if (req.headers.referer === 'http://localhost:8080/') {
const data = req.body
const user = await User.findOne({
username: data.username
})
if (!user) {
return res.send('<h1>the account doesn\'t existed!</h1 > <a href=" / ">返回</a>')
}
const isPasswordValid = require('bcrypt').compareSync(
data.password,
user.password
)
if (!isPasswordValid) {
return res.send('<h1>the password is wrong!</h1><a href=" / ">返回</a>')
}
const token = jwt.sign({
id: String(user._id),
}, SECRET)
const url = 'http://localhost:8080/comments/' + '?username=' + user.username + '&token=' + token
res.redirect(url)
}
else if (req.headers.referer === 'http://localhost:8080/admin') {
const data = req.body
const user = await User.findOne({
username: data.username
})
if (!user) {
return res.send('<h1>the account doesn\'t existed!</h1 > <a href=" / ">返回</a>')
}
if (data.level < 1) {
return res.send('<h1>the POWER account doesn\'t existed!</h1><a href=" / ">返回</a>')
}
const isPasswordValid = require('bcrypt').compareSync(
data.password,
user.password
)
if (!isPasswordValid) {
return res.send('<h1>the password is wrong!</h1><a href=" / ">返回</a>')
}
const token = jwt.sign({
id: String(user._id),
}, SECRET)
const url = 'http://localhost:8080/backyard'
res.redirect(url)
}
else if (req.headers.referer === 'http://localhost:8080/super_admin') {
const data = req.body
const user = await User.findOne({
username: data.username
})
if (!user) {
return res.send('<h1>the account doesn\'t existed!</h1 > <a href=" / ">返回</a>')
}
if (data.level < 2) {
return res.send('<h1>the POWER account doesn\'t existed!</h1><a href=" / ">返回</a>')
}
const isPasswordValid = require('bcrypt').compareSync(
data.password,
user.password
)
if (!isPasswordValid) {
return res.send('<h1>the password is wrong!</h1><a href=" / ">返回</a>')
}
const token = jwt.sign({
id: String(user._id),
}, SECRET)
const url = 'http://localhost:8080/super_backyard'
res.redirect(url)
}
})
// 留言
app.post('/submit', async (req, res) => {
const content = req.body.content
const referer = String(req.headers.referer)
if (content === '')
return res.send('<h1>Content is empty!</h1><h1>Please try again!</h1><a href=' + referer + '>返回</a>')
const username = referer.split('?')[1].split('&')[0].split('=')[1]
const submit_time = new Date().toLocaleString()
const user = await Comments.create({
username: username,
content: content,
submit_time: submit_time,
status: false
});
const tres = '<p>留言成功 等待审核</p>' + '<a href="' + referer + '">返回</a>'
res.send(tres)
})
app.get('/cmts', async (req, res) => {
// Comments.updateMany({ username: /.*/ }, { status: false }, (err, res) => {
// if (err) throw err
// console.log('ok')
// })
const data = await Comments.find()
res.send(data)
})
app.get('/users', async (req, res) => {
// User.updateMany({ username: admin }, { level: 2 }, (err, res) => {
// if (err) throw err
// console.log('ok')
// })
// User.updateOne({ username: "shichong" }, { level: 1 }, (err, res) => {
// if (err) throw err
// console.log('ok')
// })
const data = await User.find()
res.send(data)
})
const auth = async (req, res, next) => {
const raw = String(req.headers.authorization).split(' ').pop()
const { id } = jwt.verify(raw, SECRET)
req.user = await User.findById(id)
next()
}
app.get('/api/profile', auth, async (req, res) => {
res.send(req.user)
})
app.get('/comments', async (req, res) => {
if (JSON.stringify(req.query) === '{}') {
res.send('<p> 你还没有登录!</p> <a href="/">返回登录</a>')
}
else {
const { username, token } = req.query
const { id } = jwt.verify(token, SECRET)
const user = await User.findById(id)
if (user.username === username) {
res.sendFile(__dirname + '/public/comments.html')
}
}
})
app.get('/admin', (req, res) => {
res.sendFile(__dirname + '/public/index.html')
})
app.get('/super_admin', (req, res) => {
res.sendFile(__dirname + '/public/index.html')
})
app.get('/backyard', (req, res) => {
if (req.headers.referer != 'http://localhost:8080/admin' && req.headers.referer != 'http://localhost:8080/backyard')
return res.send('error')
res.sendFile(__dirname + '/public/backyard.html')
})
app.get('/super_backyard', (req, res) => {
if (req.headers.referer != 'http://localhost:8080/super_admin' && req.headers.referer != 'http://localhost:8080/super_backyard')
return res.send('error')
res.sendFile(__dirname + '/public/super_backyard.html')
})
let new_status = false
app.post('/cmtpass', async (req, res) => {
if (req.headers.referer === 'http://localhost:8080/backyard') {
const data = req.body
// console.log(data)
Comments.findOne({ username: data.username, submit_time: data.submit_time }, (err, res) => {
if (err) throw err
if (!res.status) new_status = true
else new_status = false
})
Comments.updateOne({ username: data.username, submit_time: data.submit_time }, { status: new_status }, (err, res) => {
if (err) throw err
console.log('cmt_upd')
})
}
else {
res.send('error')
}
})
app.post('/cmtdel', async (req, res) => {
if (req.headers.referer === 'http://localhost:8080/backyard') {
const data = req.body
// console.log(data)
Comments.deleteOne({ username: data.username, submit_time: data.submit_time }, (err, res) => {
if (err) throw err
console.log('cmt_del')
})
}
else {
res.send('error')
}
})
let new_level = 0
app.post('/usermod', async (req, res) => {
if (req.headers.referer === 'http://localhost:8080/super_backyard') {
const data = req.body
User.findOne({ username: data.username }, (err, res) => {
if (err) throw err
if (res.level === 1) new_level = 0
else new_level = 1
})
User.updateOne({ username: data.username }, { level: new_level }, (err, res) => {
if (err) throw err
console.log('usr_upd')
})
}
else {
res.send('error')
}
})
app.post('/userdel', async (req, res) => {
if (req.headers.referer === 'http://localhost:8080/super_backyard') {
const data = req.body
User.deleteOne({ username: data.username }, (err, res) => {
if (err) throw err
console.log('usr_del')
})
}
else {
res.send('error')
}
})
// User.collection.deleteMany({})
app.listen(8080, () => {
console.log('App listening on port 8080')
})