-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
111 lines (89 loc) · 3.47 KB
/
Copy pathserver.js
File metadata and controls
111 lines (89 loc) · 3.47 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
//modules
let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
let express = require('express');
let mongo = require("mongodb").MongoClient;
let cors = require('cors');
// configs
let config = require('./config');
//my modules
let ProductsHandler = require('./my_modules/productsHandler/ProductHandler').ProductsHandler;
let OrderHandler = require('./my_modules/ordersHandler/OrderHandler').OrderHandler;
let GetPosthadler = require('./my_modules/getPostHadler/GetPostHandler').GetPostHandle;
let TelegramBotHandler = require('./my_modules/telegramBothandler/telegramBotHandler').TelegramBotHandler;
let getPostHandler = new GetPosthadler();
//main logic
let appPath = __dirname+ '/product-app/dist/product-app';
app.use('/node_modules', express.static(__dirname + '/node_modules/'));
app.use('/images', express.static(__dirname + '/data/images'));
app.use(express.static(appPath));
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, PATCH, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
app.listen(config.server.port, function () {
console.log("server is working")
});
app.get('/', function(req, res){
res.sendFile(appPath+'/index.html');
});
app.get('/runtime.js', function(req, res){
res.sendFile(appPath+'/runtime.js');
});
app.get('/favicon.ico', function(req, res){
res.sendFile(appPath+'/favicon.ico');
});
app.get('/polyfills.js', function(req, res){
res.sendFile(appPath+'/polyfills.js');
});
app.get('/styles.js', function(req, res){
res.sendFile(appPath+'/styles.js');
});
app.get('/vendor.js', function(req, res){
res.sendFile(appPath+'/styles.js');
});
app.get('/main.js', function(req, res){
res.sendFile(appPath+'/main.js');
});
mongo.connect("mongodb://"+config.database.address, { useNewUrlParser: true }, function(err, client){
if (!err){
console.log('connect to '+config.database.name+' database');
}
else{
console.log('cant connect to database');
return;
}
let db = client.db(config.database.name);
let productsColl = db.collection(config.database.itemsCollName);
let ordersColl = db.collection(config.database.ordersCollName);
let productsHandler = new ProductsHandler(productsColl);
let orderHandler = new OrderHandler(ordersColl, productsColl);
let botHandler = new TelegramBotHandler(config.telegram.token);
app.get('/findProducts', (request, response) => {
try{
let params = getPostHandler.parseGetParams(request);
productsHandler.find(params, function (data) {
response.status(200).send(data);
})
}
catch (e) {
console.log('error in findProducts');
}
})
app.get('/makeOrder', (request, response) => {
try{
let params = getPostHandler.parseGetParams(request);
orderHandler.insertOrder(params, (obj) => {
botHandler.sendOrder(obj, params.order);
response.status(200).send("Спасибо за ваш заказ, наш оператор свяжется с вами в ближайщее время!");
});
}
catch (e) {
console.log('error in findProducts');
response.status(200).send("Сервер заказов недоступен, попробуйте позже");
}
});
});