-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·359 lines (327 loc) · 8.78 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"use strict"
var fs = require('fs');
var https = require('https');
var path = require('path');
var privateKey = fs.readFileSync('encry/server.key', 'utf8');
var certificate = fs.readFileSync('encry/server.crt', 'utf8');
var helmet = require('helmet');
var db = require("./database/db_connection");
var credentials = {
key: privateKey,
cert: certificate
};
var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
var OK = 200,
NotFound = 404,
BadType = 415;
var session = require("express-session");
var banned = [];
var bodyParser = require("body-parser");
var util = require("util");
var otype = "text/html";
var ntype = "application/xhtml+xml";
var product_id = 0;
var options = {
setHeaders: deliverXHTML
};
//content negotiation
app.use(function(req, res, next){
var otype = "text/html";
var ntype = "application/xhtml+xml";
if(path.extname(req.url) == ".html"){
var header = req.headers.accept;
var accepts = header.split(",");
var type = accepts.indexOf(ntype)>= 0 ? ntype: otype;
res.header("Content-Type", type);
}
next();
});
app.set('trust proxy', 1);
// Define the sequence of functions to be called for each request. Make URLs
// lower case, ban upper case filenames, require authorisation for admin.html,
// and deliver static files from ./public.
app.use(express.static("public", options));
app.use(session({
secret: "chyingJack",
name: 'WeisonJack',
resave: true,
saveUninitialized: true
}));
app.use(helmet());
app.use(cookieParser('secret'));
app.use(lower);
app.use(ban);
app.use("/admin.html", auth);
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function(req, res) {
// Cookies that have not been signed
console.log('Cookies: ', req.cookies)
// Cookies that have been signed
console.log('Signed Cookies: ', req.signedCookies)
})
// app.post("/admin", admin);
app.post("/login", login);
app.post("/sign", sign);
app.post("/test", test);
app.post("/productid", productid);
app.post("/buy", buy);
app.post("/basket", basket);
app.get("/detail", detail);
app.get("/product", product);
app.get('*', function(req, res) {
res.status(404).sendFile('public/images/404page.jpg', {
root: __dirname
});
})
console.log("Visit https://localhost:8080/");
banUpperCase("./public/", "");
// get the product id from the html
function productid(req, res, next) {
product_id = req.body.idp;
res.send({
code: OK
});
}
// Make the URL lower case.
function lower(req, res, next) {
req.url = req.url.toLowerCase();
next();
}
function test(req, res, next) {
res.redirect("/test.html");
}
// Forbid access to the URLs in the banned list.
function ban(req, res, next) {
for (var i = 0; i < banned.length; i++) {
var b = banned[i];
if (req.url.startsWith(b)) {
res.status(404).send("Filename not lower case");
return;
}
}
next();
}
// Redirect the browser to the login page.
function auth(req, res, next) {
res.redirect("/login.html");
}
// Called by express.static. Deliver response as XHTML.
function deliverXHTML(res, path, stat) {
// if (path.endsWith(".html")) {
// res.header("Content-Type", "application/xhtml+xml");
// }
}
// Check a folder for files/subfolders with non-lowercase names. Add them to
// the banned list so they don't get delivered, making the site case sensitive,
// so that it can be moved from Windows to Linux, for example. Synchronous I/O
// is used because this function is only called during startup. This avoids
// expensive file system operations during normal execution. A file with a
// non-lowercase name added while the server is running will get delivered, but
// it will be detected and banned when the server is next restarted.
function banUpperCase(root, folder) {
var folderBit = 1 << 14;
var names = fs.readdirSync(root + folder);
for (var i = 0; i < names.length; i++) {
var name = names[i];
var file = folder + "/" + name;
if (name != name.toLowerCase()) banned.push(file.toLowerCase());
var mode = fs.statSync(root + file).mode;
if ((mode & folderBit) == 0) continue;
banUpperCase(root, file);
}
}
function login(req, res) {
var username = req.body.username;
var password = req.body.password;
var sql1 = util.format("select * from user where username = '%s' and password = '%s'", username, password);
db.db_connectionAll(sql1, function(err, row) {
if (err) {
res.send({
code: BadType,
msg: "This username has been taken, please use another one!"
});
return;
}
if (row.length <= 0) {
res.send({
code: BadType,
msg: "There is no such username or your password is error, please try again!"
});
return;
}
req.session.login = true;
req.session.username = username;
res.cookie('nick', username);
res.send({
code: OK
});
});
}
function sign(req, res) {
var sql1 = util.format("insert into user values('%s', '%s', '%s', '%s', '%s', '%s')", req.body.username, req.body.password, req.body.name, req.body.birthday, req.body.address, req.body.phone);
db.db_connectionRun(sql1, function(err, row) {
if (err) {
console.log("insert error");
res.send({
code: BadType,
msg: "Database insert user is error! "
});
return;
} else {
res.send({
code: OK
});
}
})
}
function detail(req, res) {
var loginSign = req.session.login;
if (!loginSign) {
res.send({
code: NotFound,
msg: "You haven't logged in yet!"
});
return;
}
var username = req.session.username;
var sql1 = "select * from user where username = '" + username + "'";
db.db_connectionAll(sql1, function(err, row) {
if (err) {
res.send({
code: BadType,
msg: "The query of Database is ERROR!"
});
throw err;
return;
}
if (row.length <= 0) {
res.send({
code: BadType,
msg: "There is no information of this user"
});
return;
}
var data = row[0];
res.send({
code: OK,
user: data.username,
name: data.name,
birt: data.birthday,
addr: data.address,
tele: data.phone
});
})
}
function product(req, res) {
var sql1 = util.format("select * from product where id = '%d'", product_id);
db.db_connection(sql1, function(err, row) {
if (err) {
res.send({
code: BadType,
msg: "The query of Database is ERROR!"
});
throw err;
return;
}
if (row == null) {
res.send({
code: BadType,
msg: "There is no information of this product!"
});
return;
}
var data = row;
res.send({
code: OK,
id: data.id,
title: data.title,
brand: data.brand,
price: data.price,
memory: data.memory,
storage: data.storage,
processor: data.processor,
screen_size: data.screen_size,
graphics: data.graphics,
image: data.image
});
})
}
function buy(req, res) {
var loginSign = req.session.login;
if (!loginSign) {
res.send({
code: NotFound,
msg: "You haven't logged in yet! Please login in first."
});
return;
}
var sql1 = util.format("insert into basket values('%s', '%s', '%d')", req.body.productid, req.session.username, req.body.quantity);
if (req.body.quantity == 0) {
res.send({
code: OK
});
return;
}
db.db_connectionRun(sql1, function(err, row) {
if (err) {
console.log("insert error");
res.send({
code: BadType,
msg: "Database insert basket is error! "
});
return;
} else {
res.send({
code: OK
});
}
})
}
function basket(req, res) {
var loginSign = req.session.login;
if (!loginSign) {
res.send({
code: NotFound,
msg: "You haven't logged in yet! Please login in first."
});
return;
}
var sql = util.format("SELECT * FROM (select SUM(quantity) AS sum, product_id AS pid, user_id AS uid from basket where user_id = '%s' group by product_id ) AS a JOIN product p ON a.pid = p.id JOIN user u ON u.username = a.uid", req.session.username);
db.db_connectionAll(sql, function(err, rows) {
if (err) {
res.send({
code: BadType,
msg: "The query of Database is ERROR!"
});
throw err;
return;
}
if (rows == null) {
res.send({
code: BadType,
size: 0,
msg: "There is no information of your basket!"
});
return;
}
var len = rows.length;
var data = new Array(len);
var total = new Array(len);
for (var i = 0; i < rows.length; i++) {
data[i] = rows[i];
total[i] = data[i].sum * data[i].price;
}
res.send({
code: OK,
size: len,
amount: total,
row: data,
});
})
}
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(8080);