-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathserver.js
101 lines (86 loc) · 3.2 KB
/
server.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
/* eslint-disable */
require('es6-promise').polyfill(); // Required in some browsers
//babel polyfill ES6
require('babel-core/register')();
require.extensions['.scss'] = function () { return; };
require.extensions['.css'] = function () { return; };
var express = require('express');
var path = require('path');
var compression = require('compression');
const app = express();
var http = require('http').Server(app);
var error_handling_middleware = require('./app/api/utils/error_handling_middleware.js');
var privateInstanceMiddleware = require('./app/api/auth/privateInstanceMiddleware.js');
var bodyParser = require('body-parser');
var winston = require('winston'),
expressWinston = require('express-winston');
// app.use(expressWinston.logger({
// transports: [
// new winston.transports.File({
// name: 'access',
// filename: './log/access.log',
// json: false,
// handleExceptions: true,
// level: 'debug'
// })
// ]
// }));
app.use(error_handling_middleware);
app.use(compression());
var oneYear = 31557600;
var maxage = 0;
if (app.get('env') === 'production') {
maxage = oneYear;
}
app.use(express.static(path.resolve(__dirname, 'dist'), {maxage: maxage}));
app.use('/public', express.static(path.resolve(__dirname, 'public')));
app.use(bodyParser.json());
require('./app/api/auth/routes.js')(app);
app.use(privateInstanceMiddleware);
app.use('/uploaded_documents', express.static(path.resolve(__dirname, 'uploaded_documents')));
app.use('/flag-images', express.static(path.resolve(__dirname, 'dist/flags')));
require('./app/api/api.js')(app, http);
require('./app/react/server.js')(app);
var dbConfig = require('./app/api/config/database');
var translations = require('./app/api/i18n/translations.js');
var systemKeys = require('./app/api/i18n/systemKeys.js');
var ports = require('./app/api/config/ports.js');
const port = ports[app.get('env')];
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.File({
name: 'error',
filename: './log/error.log',
prettyPrint: true,
json: false,
handleExceptions: true,
humanReadableUnhandledException: true,
level: 'error'
})
]
}));
process.on('uncaughtException', err => winston.error('uncaught exception: ', err));
process.on('unhandledRejection', (reason, p) => winston.error('unhandled rejection: ', reason, p));
var mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect(dbConfig[app.get('env')], {useMongoClient: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.info('==> Processing system keys...')
translations.processSystemKeys(systemKeys)
.then(function() {
http.listen(port, '0.0.0.0', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.', port, port);
if (process.env.HOT) {
console.info('');
console.info('webpack is watching...');
console.info(require('./message'));
}
});
})
.catch(console.log);
});