-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
127 lines (105 loc) · 3.6 KB
/
app.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
const express = require('express');
const path = require('path');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const rateLimit = require('express-rate-limit');
const logger = require('./lib/logger');
const routes = require('./routes/index');
/**
* Creates an app that can be used with an http.server.
* @param config: the config object that specifies the details of the app.
* @returns {*}
*/
function createApp(config) {
// Set default in config.
config.port = config.port || 4000;
config.logLevel = config.logLevel || 'info';
config.baseURL = config.baseURL || 'http://localhost:' + config.port;
config.basePath = config.basePath || '/';
config.removeTempFolders = config.removeTempFolders === undefined || config.removeTempFolders;
if (!config.basePath.startsWith('/')) {
config.basePath = '/' + config.basePath;
}
// Set up app.
const app = express();
app._basePath = config.basePath;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
/* istanbul ignore next */
// configure logger
if (config.logLevel) {
const temp = config.logLevel === 'debug' ? 'dev' : 'combined';
app.use(morgan(temp));
}
// allow CORS
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
// configure the body of the requests
app.use(bodyParser.json({
extended: false,
limit: '50mb'
}));
/* istanbul ignore next */
if (config.behindReverseProxy) {
// Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
// see https://expressjs.com/en/guide/behind-proxies.html
app.set('trust proxy', 1);
}
/* istanbul ignore next */
if (config.rateLimiter) {
const limiter = rateLimit({
windowMs: config.rateLimiter.window * 60 * 1000,
max: config.rateLimiter.max // limit each IP to "max" requests per windowMs
});
// apply to execute requests
app.use('/execute', limiter);
logger.info(`Rate limiter enabled: max ${config.rateLimiter.max} request per ${config.rateLimiter.window} minutes.`);
}
// configure where to find public files
app.use(config.basePath, express.static(path.join(__dirname, 'public')));
// add the routes
app.use(config.basePath, routes(config));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
/* istanbul ignore next */
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
/* istanbul ignore next */
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
return app;
}
module.exports = createApp;