forked from origo-map/origo-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (75 loc) · 3.13 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
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var cors = require('cors');
const rateLimit = require('express-rate-limit');
var routes = require('./routes/index');
var mapStateRouter = require('./routes/mapstate');
var errors = require('./routes/errors');
var conf = require('./conf/config');
var app = express();
const limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 10000, // Limit each IP to 10000 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
// apply rate limiter to all requests
app.use(limiter);
var server = app.listen(3001, function () {
var host = server.address().address
var port = server.address().port
console.log('Origo server listening at http://%s:%s', host, port)
});
//Workaround to set __dirname properly when using node-windows
process.chdir(__dirname);
var handlebars = require('express-handlebars')
.create({ defaultLayout: 'main', helpers: {
eq: function (v1, v2) { return v1 === v2; },
ne: function (v1, v2) { return v1 !== v2; },
lt: function (v1, v2) { return v1 < v2; },
gt: function (v1, v2) { return v1 > v2; },
lte: function (v1, v2) { return v1 <= v2; },
gte: function (v1, v2) { return v1 >= v2; },
and: function () { return Array.prototype.every.call(arguments, Boolean); },
or: function () { return Array.prototype.slice.call(arguments, 0, -1).some(Boolean); }
}});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views'));
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json({limit: "5mb"}));
app.use(bodyParser.urlencoded({limit: "5mb", extended: true, parameterLimit:50000}));
app.use(express.json({limit: '5mb'}));
app.use(express.urlencoded({limit: '5mb', extended: true, parameterLimit:50000}));
app.use(express.static(path.join(__dirname, 'public')));
if (conf['cors']) {
var configOptions = Object.assign({}, conf['cors']);
var corsOptions = {};
// Configures the Access-Control-Allow-Origin CORS header.
if ('origin' in configOptions) {
corsOptions['origin'] = configOptions.origin;
}
// Configures the Access-Control-Allow-Methods CORS header.
if ('methods' in configOptions) {
corsOptions['methods'] = configOptions.methods;
}
// Configures the Access-Control-Allow-Headers CORS header.
if ('headers' in configOptions) {
corsOptions['allowedHeaders'] = configOptions.headers;
}
// Configures the Access-Control-Allow-Credentials CORS header.
if ('credentials' in configOptions) {
corsOptions['credentials'] = configOptions.credentials;
}
// Some legacy browsers (IE11, various SmartTVs) choke on 204
if ('optionsSuccessStatus' in configOptions) {
corsOptions['optionsSuccessStatus'] = configOptions.optionsSuccessStatus;
}
app.use(cors(corsOptions));
}
app.use('/origoserver/', routes);
app.use('/mapstate', mapStateRouter);
app.use(errors);
module.exports = app;