-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·40 lines (33 loc) · 1.21 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
const express = require('express');
const winston = require('winston');
const expressWinston = require('express-winston');
const https = require('https');
const fs = require('fs');
const config = require('./src/config.js');
const routes = require('./src/router');
require('dotenv').config();
const app = express();
require('./src/middleware')(app);
app.use('/v2', routes);
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console(),
],
requestWhitelist: ['url', 'method', 'httpVersion', 'originalUrl', 'query'],
format: winston.format.combine(
winston.format.json(),
),
}));
const logUrl = (protocol) => {
console.log(`Server started | ENV=${config.env} | ${protocol}://localhost:${config[protocol].port}`);
};
if (config.protocol === 'http') {
this.http = app.listen(config.http.port || 8081, config.http.host, logUrl('http'));
}
if (config.protocol === 'https') {
const key = fs.readFileSync(config.https.key, 'utf8');
const cert = fs.readFileSync(config.https.cert, 'utf8');
const ca = config.https.ca ? fs.readFileSync(config.https.ca, 'utf8') : undefined;
this.https = https.createServer({ key, cert, ca }, app)
.listen(config.https.port || 8443, logUrl('https'));
}