-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
349 lines (305 loc) · 12.3 KB
/
app.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
let express = require('express');
let exP = express();
const body_parser = require('body-parser'); //парсить данные из формы
let cookieParser = require('cookie-parser');
let admin = require('./admin');
const bcrypt = require('bcrypt');
const dotenv = require('dotenv');
dotenv.config();
/* запуск сервера */
const port = process.env.PORT || 3002;
exP.listen(port,()=>{console.log(`server's listening on port ${port}`)});
// параметр безопасности, чтобы писать в базу ?!!
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
/* задаём папку со статикой */
exP.use(express.static('public'));
/* задаём метод чтения ответа ?!?!?! а-ля body parser */
exP.use(express.json());
exP.use(body_parser.urlencoded({extended:true})); //парсить данные из формы
exP.use(cookieParser());
/* задаём шаблонизатор */
exP.set('view engine','pug');
/* подключаем nodemailer */
const nodemailer = require('nodemailer');
/* my Sql */
const mysql2 = require('mysql2');
/** Делаем отказоустойчивый polling БД, чтобы программа не падала */
let conn;
function handleDisconnect() {
conn = mysql2.createConnection({
host: process.env.MYSQLHOST,
//host: process.env.DB_HOST, // для контейнера с БД MySQL. См docker-compose.yml
port: process.env.MYSQLPORT,
user: process.env.MYSQLUSER,
password: process.env.MYSQLPASSWORD,
database: process.env.MYSQLDATABASE,
});
conn.connect(function(err) {
if(err) {
console.log('Error connecting to MySQL database:', err);
setTimeout(handleDisconnect, 2000);
}
});
conn.on('error', function(err) {
console.log('MySQL connection error:', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect();
} else {
throw err;
}
});
}
handleDisconnect();
/* ---middleware самописное - 1 штука--УРОВНЯ ПРИЛОЖЕНИЯ*/
const adminWays = ['/admin','/admin-order'];
exP.use(function (req, res, next) {
if (adminWays.includes(req.originalUrl)) {
//if (req.originalUrl == '/admin' || req.originalUrl == '/admin-order') {
admin(req, res, conn, next);
}
else {
next();
}
});
exP.get("/",function (req,res){
let cat = new Promise(function (resolve, reject) {
conn.query(
"select id, slug, name, cost, image, category from (select id,slug,name,cost,image,category, if(if(@curr_category != category, @curr_category := category, '') != '', @k := 0, @k := @k + 1) as ind from goods, ( select @curr_category := '' ) v ) goods where ind < 3",
function (error, result, field) {
if (error) return reject(error);
resolve(result);
}
);
});
let catDescription = new Promise(function (resolve, reject) {
conn.query(
"SELECT * FROM category",
function (error, result, field) {
if (error) return reject(error);
resolve(result);
}
);
});
Promise.all([cat, catDescription]).then(function (value) {
//console.log(value[1]);
res.render('shop', {
goods: JSON.parse(JSON.stringify(value[0])),
cat: JSON.parse(JSON.stringify(value[1])),
});
});
});
exP.get("/cat",function (request,response){
let id = request.query.id || 1;
let cat = new Promise((resolve,reject)=>{
conn.query("SELECT * FROM category WHERE id="+id, function (err,queryResult){
if(err){
reject(err)
}
resolve(queryResult)
})
});
let goodsByCat = new Promise((resolve,reject)=>{
conn.query("SELECT * FROM goods WHERE category="+id, function (err,queryResult){
if(err){
reject(err)
}
resolve(queryResult)
})
});
Promise.all([cat, goodsByCat]).then(resultArray =>{
response.render('cat',{
categ:resultArray[0],
goodsByCateg:resultArray[1]
})
})
});
exP.get('/item/*',function(request,response){
//console.log(request.params)
let slug = request.params[0]
conn.query("SELECT * FROM goods WHERE slug="+`"${slug}"`, function (err,result, fields) {
if(err) throw err
//console.log('result from axp get item',result);
conn.query('SELECT * FROM images WHERE goods_id=' + result[0]['id'], function (error, goodsImages, fields) {
if (error) throw error;
response.render('item', { item: result, goods_images: goodsImages });
});
})
});
exP.get('/order', function (req, res) {
res.render('order');
});
exP.post('/get-category-list',(request, response)=>{
conn.query("SELECT id,category FROM category", (err,result,fields)=>{
if (err) throw err;
response.json(result)
})
});
exP.post('/get-goods-info',(request, response)=>{
let reqKeysArray = request.body.key; // ['4','5']
if (reqKeysArray.length !== 0){
let keysString = reqKeysArray.join(',')// 4,5
conn.query('SELECT id,name,cost FROM goods WHERE id IN ('+keysString+')', function (error, result, fields) {
if (error) throw error;
// result - это неудобный массив: [ { id: 4,name, cost},{ id: 7,name, cost}]
let goods = {};
result.forEach((elem, index)=>{
goods[elem.id] = elem;
});
// goods - это удобный (индексированный) массив: [ 4:{ id: 4,name, cost},7:{ id: 7,name, cost}]
response.json(goods);
});
}
else response.send('0')
});
exP.post('/finish-order',(request, response)=>{
if (request.body.key.length !== 0){
let key = Object.keys(request.body.key);
conn.query(
'SELECT id,name,cost FROM goods WHERE id IN (' + key.join(',') + ')',
function (error, result, fields) {
if (error) throw error;
//console.log('result from finish-order = ', result);
sendMail(request.body, result).catch(console.error);
saveOrder(request.body, result);
response.send('1');
});
}
else response.send('0')
})
exP.get('/admin', function (req, res) {
res.render('admin', {});
});
exP.get('/admin-order', function (req, res) {
conn.query(`SELECT
shop_order.id as id,
shop_order.user_id as user_id,
shop_order.goods_id as goods_id,
shop_order.goods_cost as goods_cost,
shop_order.goods_amount as goods_amount,
shop_order.total as total,
from_unixtime(date,"%Y-%m-%d %h:%m") as human_date,
user_info.user_name as user,
user_info.user_phone as phone,
user_info.address as address
FROM
shop_order
LEFT JOIN
user_info
ON shop_order.user_id = user_info.id ORDER BY id DESC`, function (error, result, fields) {
if (error) throw error;
//console.log(result);
res.render('admin-order', { order: JSON.parse(JSON.stringify(result)) });
});
});
/**
* login form ==============================
*/
exP.get('/login', function (req, res) {
res.render('login', {});
});
exP.post('/login', function (req, res) {
//console.log(`req body = ${req.body} \n req.body.login = ${req.body.login} \n req.body.password = ${req.body.password}`);
// ПЕРЕДЕЛАНО "ПОД СЕССИИ" (уникальный UUID)
// запрос на поиск строки с хешем в базе по логину
let getAdminQuery = `SELECT * FROM user WHERE login="${req.body.login}"`;
//console.log('get hash query = ',getAdminQuery)
conn.query(
getAdminQuery,
function (error, result) {
if (error) throw (error);
if (result.length === 0) {
//console.log('error user not found');
res.redirect('/login');
}
else if(result.length !== 0){
//console.log('app.js full string from DB result = ',result);
let downloadedHash = result[0].hash
//console.log('doloaded hash = ',downloadedHash)
//console.log('req.body.password = ',req.body.password)
bcrypt.compare(req.body.password, downloadedHash).then(answer => {
if(answer){
//console.log('уря,совпало!!!')
let UUID = makeHash(req.body.password+req.body.login);
res.cookie('UUID', UUID);
res.cookie('login', result[0].login);
/**
* write UUID to db
*/
let uuidWriteQuery = `UPDATE user SET UUID="${UUID}" WHERE login="${result[0].login}"`;
conn.query(uuidWriteQuery, function (error, resultQuery) {
if (error) throw error;
res.redirect('/admin');
});
} else {
res.render('login',{
stats:'wrong password'
})
}
})
}
});
});
//data = req.body = данные заказчика из формы + id/count из cart
//result / positionsList = ответ с базы данных по запросу = товары, найденные по id
function saveOrder(data, result) {
//1 - в юзер-таблицу
let {username,phone,address,email,key} = data;
let usersQuery = "INSERT INTO user_info (user_name,user_phone,user_email,address) VALUES (" +
`"${username}", "${phone}", "${address}", "${email}"` + ")";
conn.query(usersQuery, (err,result)=>{
if(err) throw err;
console.log('1 user wrote to DB : \n', result);
});
//2 - в гудс таблицу
let date = new Date()/1000;
result.forEach(item => {
let shopOrderQuery =
"INSERT INTO shop_order (date, user_id, goods_id, goods_cost, goods_amount, total) VALUES (" +
`"${date}", "12", "${item.id}", "${item.cost}", "${key[item.id]}", "${key[item.id] * item.cost}"` + ")";
conn.query(shopOrderQuery, (err,result)=>{
if(err) throw err;
console.log('--------- \n 1 order recorded to DB: \n',result);
})
})
}
//в sendMail передаются те же данные, что и в saveOrder
async function sendMail(data, result) {
let res = '<h2>Order in lite shop</h2>';
let total = 0;
result.forEach(el => {
res += `<p>${el['name']} - ${data.key[el['id']]} - ${el['cost'] * data.key[el['id']]} uah</p>`;
total += el['cost'] * data.key[el['id']];
})
console.log(res);
res += '<hr>';
res += `Total ${total} uah`;
res += `<hr>Phone: ${data.phone}`;
res += `<hr>Username: ${data.username}`;
res += `<hr>Address: ${data.address}`;
res += `<hr>Email: ${data.email}`;
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass // generated ethereal password
}
});
let mailOption = {
from: '<[email protected]>',
to: "[email protected]," + data.email,
subject: "Lite shop order",
text: 'Hello world',
html: res
};
let info = await transporter.sendMail(mailOption);
console.log("MessageSent: %s", info.messageId);
console.log("PreviewSent: %s", nodemailer.getTestMessageUrl(info));
return true;
}
function makeHash(password) {
const salt = bcrypt.genSaltSync(10);
return bcrypt.hashSync(password,salt);
}