-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (146 loc) · 8.48 KB
/
index.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
import { createResponse } from './api/helpers/response';
const express = require('express');
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const helmet = require('helmet');
const moment = require('moment');
const timeout = require('connect-timeout'); //express v4
const config = require('./conf');
const session = require('express-session');
const logger = require('./api/helpers/logger');
const Store = require('./api/helpers/store');
const pako = require('pako');
let server;
app.set('trust proxy', 1);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.set('port', (process.env.PORT || 5000));
app.use('/', express.static(__dirname + '/dist'));
const exitHandler = function (options, err) {
if (err) {
console.log(err);
logger.error(moment().format("DD.MM.YYYY HH:mm") + ": " + err.stack);
}
if (options.exit) {
if (server && server.close) {
server.close();
}
process.exit(0);
}
};
process.on('uncaughtException', exitHandler.bind(null, {exit: false}));
process.on('SIGTERM', exitHandler.bind(null, {exit: true}));
//do something when app is closing
process.on('exit', exitHandler.bind(null, {exit: true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit: true}));
process.on("SIGUSR1", exitHandler.bind(null, {exit: true}));
// Persist sessions with mongoStore
app.use(['/api*'], session({
secret: 'cdek-secret',
cookie: {path: '/', httpOnly: true, secure: false, maxAge: null },
resave: true,
saveUninitialized: true,
rolling: false
}));
app.use(function (err, req, res, next) {
const contentType = req.headers['content-type'];
if (req.xhr || (!contentType || contentType && contentType.indexOf('json') > -1)) {
createResponse(res, err);
} else {
next(err);
}
});
app.options('/api/*', cors());
app.get('/api/tariff/request', require('./api/tariff'));
app.post('/api/tariff/one', cors(), require('./api/tariff/one'));
// app.post('/api/tariff/news', cors(), require('./api/news'));
app.get('/api/settings', cors(), require('./api/settings'));
app.post('/api/beacon', cors(), (req, res) => {
Store.delete(req);
res.end();
});
if (process.env.NODE_ENV === 'development') {
require('./api/tariff')(
{
headers: {},
query: {
data: Buffer.from(pako.deflate(JSON.stringify({
deliveries: ['dpd'],
cities: [
{from: 'Яхрома, Дмитровский район, Московская обл.', to: 'Новосибирск', countryFrom: ''},
// {from: 'Челябинск', to: 'Владивосток', countryFrom: ''},
// {from: 'Яхрома, Дмитровский район, Московская обл.', to: 'Новосибирск', countryFrom: ''},
// {from: 'Минск', to: 'Гомель', countryFrom: 'Беларусь', countryTo: 'Беларусь'}
// {from: 'Пинск', to: 'Мозырь', countryFrom: 'Беларусь', countryTo: 'Беларусь'}
// {from: 'Минск', postcodeFrom: '119002', to: 'Москва', countryFrom: 'Беларусь', countryTo: ''}
// {from: 'Алматы', to: 'Нур-Султан', countryFrom: 'Казахстан', countryTo: 'Казахстан', countryFromEngShort: "KZ", countryToEngShort: "KZ", fromEngName: "Almaty", toEngName: 'Nur-Sultan 020000'},
// {from: 'Алматы', to: 'Атырау', countryFrom: 'Казахстан', countryTo: 'Казахстан'},
// {from: 'Алматы', to: 'Сидней', countryFrom: 'Казахстан', countryTo: 'Австралия'},
// {from: 'Алматы', to: 'Москва', fromEngName: "Almaty", toEngName: 'Moscow', countryFrom: 'Казахстан', countryTo: '', countryFromEngShort: "KZ", countryToEngShort: "RU"},
// {from: 'Сидней', postcodeFrom: '119002', to: 'Минск', countryFrom: 'Австралия', countryTo: 'Беларусь'},
// {from: 'Минск', postcodeFrom: '119002', to: 'Сидней', countryFrom: 'Беларусь', countryTo: 'Австралия'},
// {from: 'Минск', postcodeFrom: '119002', to: '', countryFrom: 'Беларусь', countryTo: 'Австралия'}
// {from: 'Москва', to: 'Новосибирск', postcodeFrom: "109012", postcodeTo: "630064", countryFromEngShort: 'RU', countryToEngShort: 'RU'},
// {from: 'Яхрома, Дмитровский район, Московская обл.', to: 'Новосибирск', countryFrom: ''},
// {from: 'Москва', to: 'Сидней', countryTo: 'Австралия', postcodeFrom: "109012", postcodeTo: "2000", countryFromEngShort: 'RU', countryToEngShort: 'AU', fromEngName: "Moscow", toEngName: "Sydney NSW"},
],
weights: [1]
}))).toString('base64')
},
socket: {
setTimeout: function () {},
},
},
{
end: function () {},
set: function () {},
write: (v) => {
},
on: function () {},
flushHeaders: function () {},
},
);
}
/*require('./api/tariff')(
{session: {delivery: {}}, body: {
deliveries: ['ponyexpressby'],
cities: [
//{from: 'Пушкино, Московская обл.', to: 'Москва'},
//{from: 'Москва', to: 'Sydney', countryTo: 'Australia'},
//{from: 'Новосибирск', to: 'Москва', postcodeFrom: '630000', countryFromEngShort: 'RU', countryToEngShort: 'RU', postcodeTo: '119002', fromGooglePlaceId: 'ChIJl03MkOHl30IRhenT4XMGOps', toGooglePlaceId: 'ChIJybDUc_xKtUYRTM9XV8zWRD0', fromEngName: "Novosibirsk", fromEngFullName: "Novosibirsk, Novosibirsk Oblast, Russia", toEngName: "Moscow", toEngFullName: "Moscow, Russia"},
//{from: 'Новосибирск', to: 'Москва', postcodeFrom: '119002', countryFromEngShort: 'RU', countryToEngShort: 'RU', postcodeTo: '630000', fromGooglePlaceId: 'ChIJl03MkOHl30IRhenT4XMGOps', toGooglePlaceId: 'ChIJybDUc_xKtUYRTM9XV8zWRD0', fromEngName: "Novosibirsk", fromEngFullName: "Novosibirsk, Novosibirsk Oblast, Russia", toEngName: "Moscow", toEngFullName: "Moscow, Russia"},
//{from: 'Пушкино, Московская обл.', to: 'Новосибирск'},
//{from: 'Москва', to: 'Новосибирск'},
//{from: 'Челябинск', to: 'Владивосток'},
//{from: 'Москва', to: 'Бангкок'},
//{from: 'Москва', to: 'Абай', countryTo: 'Казахстан'},
//{from: 'Москва', postcodeFrom: '630000', to: '', countryTo: 'Азербайджан'},
//{from: 'Москва', postcodeFrom: '630000', to: '', countryTo: 'Австралия'},
{from: 'Минск', postcodeFrom: '119002', to: 'Гомель', countryFrom: 'Беларусь', countryTo: 'Беларусь'}
// {from: 'Минск', postcodeFrom: '119002', to: 'Москва', countryFrom: 'Беларусь', countryTo: ''},
// {from: 'Минск', postcodeFrom: '119002', to: 'Минск', countryFrom: 'Беларусь', countryTo: 'Беларусь'},
// {from: 'Сидней', to: 'Астана', countryFrom: 'Австралия', countryTo: 'Казахстан'},
// {from: 'Алматы', to: 'Астана', countryFrom: 'Казахстан', countryTo: 'Казахстан'},
// {from: 'Алматы', to: 'Алматы', countryFrom: 'Казахстан', countryTo: 'Казахстан'},
// {from: 'Алматы', to: 'Москва', countryFrom: 'Казахстан', countryTo: ''},
// {from: 'Астана', to: 'Москва', countryFrom: 'Казахстан', countryTo: '', countryFromEngShort: 'KZ', countryToEngShort: 'RU', fromEngName: 'Astana', toEngName: 'Moscow'},
// {from: 'Астана', to: 'Киев', countryFrom: 'Казахстан', countryTo: 'Украина'},
// {from: 'Алматы', to: 'Абакан', countryFrom: 'Казахстан', countryTo: ''},
// {from: 'Алматы', to: 'Минск', countryFrom: 'Казахстан', countryTo: 'Беларусь'},
],
weights: [1]
}},
{status: function () {return {json: function () {}}}},
{json: function () {}}
);*/
//require('./api/news')(
// {session: {delivery: {}}, body: {delivery: 'baikalsr', date: require('moment')().add(-3, 'month')}},
// {json: function () {}}
//);
server = app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});