-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (58 loc) · 1.88 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
require('log-timestamp');
require('dotenv').load();
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const app = require('express')();
app.use(bodyParser.urlencoded({ "extended": true }));
app.use(bodyParser.json());
// Enable CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header("Access-Control-Allow-Methods", "HEAD, GET, POST, PUT, DELETE");
next();
});
// Additional custom validators TODO: this should go in a separated files in /common
app.use(expressValidator({
"customValidators": {
"isArray"(value) {
return Array.isArray(value);
},
"isFormat"(value) {
return value === 'png' || value === 'jpeg';
},
"isNotEmpty"(value) {
return Object.keys(value).length !== 0 && value.constructor === Object;
},
"isObject"(value) {
if (value === null) {
return false;
}
return typeof value === 'function' || typeof value === 'object';
}
}
}));
const serverConfig = require('./serverConfig');
let server;
if (serverConfig.https) {
const http = require('https');
const fs = require('fs');
server = http.createServer({
"pfx": fs.readFileSync(serverConfig.ssl.pfx),
"passphrase": serverConfig.ssl.passphrase
}, app);
} else {
const http = require('http');
server = http.createServer(app);
}
const websockets = require('./websockets/index');
const WS = new websockets(server);
app.use('/', (req, res, next) => {
req.courierSocket = WS;
next();
}, require('./routes/eventManager'));
app.use(require('./middlewares/errorHandlers').logError);
app.use(require('./middlewares/errorHandlers').errorHandler);
server.listen(serverConfig.port, () => {
console.log(`Courier is listening on port ${serverConfig.port}...`);
});