-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
85 lines (70 loc) · 2.24 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
'use strict';
// register project root to global require calls
require('app-module-path/register');
const express = require('express');
const app = module.exports = express();
// const config = require('./config/config');
// const swaggerJSDoc = require('swagger-jsdoc');
const compression = require('compression');
const favicon = require('serve-favicon');
const log = require('./lib/logger');
const testData = require('testData');
const CircularJSON = require('circular-json');
// swagger definition
// const swaggerDefinition = {
// info: {
// title: 'API Documentation',
// version: '1.0.0',
// description: 'Docs for formula-AST rendering API',
// },
// host: config.host,
// basePath: '/',
// };
// initialize swagger-jsdoc
// const swaggerSpec = swaggerJSDoc({
// swaggerDefinition,
// apis: ['./api/v1/routes/*.js'],
// });
app.use(favicon(__dirname + '/frontend/favicon.ico'));
// compress all assets and json-responses if minimal size is reached
app.use(compression());
app.locals = testData.locals;
// Allow 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');
next();
});
app.use(express.static('./frontend/public'));
// expose routes for templates
app.use('/', require('./routes/routes'));
// expose api
app.use('/api', require('./api/versions'));
// expose api docs
app.use('/api/docs', express.static('./config/apiDoc'));
// serve swagger
// app.get('/swagger.json', (req, res) => {
// res.setHeader('Content-Type', 'application/json');
// res.send(swaggerSpec);
// });
// global errorHandler ============================================
require('./errorHandler/ErrorHandler')(app);
// set up server ==================================================
function start() {
return app.listen(process.env.PORT || 4001, () => {
log.info(`server started, listening on port: ${process.env.PORT || 4001}`);
process.on('unhandledRejection', (reason) => {
try{
log.info(reason.stack);
} catch (e) {}
log.info('Unhandled promise rejection. Reason:', CircularJSON.stringify(reason));
});
});
}
if (require.main === module) {
start();
}
module.exports = {
start,
app
};