-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
207 lines (159 loc) · 7.39 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// jshint esversion: 8
// jshint node: true
// jshint maxerr: 1000
"use strict"; // JavaScript code is executed in "strict mode"
/**
* @desc TwittStorm, Geosoftware 2, WiSe 2019/2020
* @author Jonathan Bahlmann, Katharina Poppinga, Benjamin Rieke, Paula Scharf
*/
const http = require('http');
const https = require('https');
const path = require('path');
var express = require('express');
var app = express();
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
const mongodb = require('mongodb'); // using the mongo-driver
var JL = require('jsnlog').JL;
var jsnlog_nodejs = require('jsnlog-nodejs').jsnlog_nodejs;
var logger = require('morgan');
// for using variables in .env file
require('dotenv').config();
// R
const R = require('r-script');
// yaml configuration
const fs = require('fs');
const yaml = require('js-yaml');
const config = yaml.safeLoad(fs.readFileSync('config.yaml', 'utf8'));
// set the routers-paths
var indexRouter = require('./routes/index');
var warningsRouter = require('./routes/warnings');
var radarRouter = require('./routes/radar');
var twitterRouter = require('./routes/twitter');
var configRouter = require('./routes/configuration').router;
var animationRouter = require('./routes/animation');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')));
// logging HTTP requests
app.use(logger('dev'));
// load/provide all files given in the folder public
app.use(express.static(path.join(__dirname, 'public')));
// use built-in middleware which parses incoming requests with JSON payloads so that explicit parse expressions for every JSON are not necessary
app.use(express.json());
// use built-in middleware which parses urlencoded bodies, https://expressjs.com/en/4x/api.html#express.urlencoded
app.use(express.urlencoded({ extended: false }));
// set the routes for npm-installed client-libraries
app.use("/jquery", express.static(path.join(__dirname, 'node_modules', 'jquery', 'dist')));
app.use("/bootstrap", express.static(path.join(__dirname, 'node_modules', 'bootstrap', 'dist')));
app.use("/popper", express.static(path.join(__dirname, 'node_modules', '@popperjs', 'core', 'dist')));
app.use("/mapbox", express.static(path.join(__dirname, 'node_modules', 'mapbox-gl', 'dist')));
app.use("/mapbox-draw", express.static(path.join(__dirname, 'node_modules', '@mapbox', 'mapbox-gl-draw', 'dist')));
app.use("/mapbox-geocoder", express.static(path.join(__dirname, 'node_modules', '@mapbox', 'mapbox-gl-geocoder', 'dist')));
app.use("/turf", express.static(path.join(__dirname, 'node_modules', '@turf', 'turf')));
app.use("/jsnlog", express.static(path.join(__dirname, 'node_modules', 'jsnlog')));
app.use("/R", express.static(path.join(__dirname, 'node_modules', 'r-script', )));
app.use("/gifshot", express.static(path.join(__dirname, 'node_modules', 'gifshot', 'dist')));
app.use("/downloadjs", express.static(path.join(__dirname, 'node_modules', 'downloadjs')));
app.use("/html2canvas", express.static(path.join(__dirname, 'node_modules', 'html2canvas', 'dist')));
// ***************************** mongo-database *******************************
// middleware for making the db connection available via the request object
app.use((req, res, next) => {
req.db = app.locals.db;
next();
});
/**
* Try to connect to mongodb on localhost:27017 (if not using docker),
* if not possible try to connect on mongodbservice:27017 (if using docker),
* if not possible print the error.
*/
function connectMongoDb() {
(async () => {
// try to connect to mongodb on localhost:27017
try {
// await blocks and waits for connection, because here synchronous execution is desired
app.locals.dbConnection = await mongodb.MongoClient.connect(
// connectionString / connection URL:
"mongodb://" + config.mongodb.host + ":" + config.mongodb.port,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
);
// connect to and use database config.mongodb.db_name (create this database, if it does not exist)
app.locals.db = await app.locals.dbConnection.db(config.mongodb.db_name);
// tell the user that the connection is established and database config.mongodb.db_name will be used for following operations
console.log("Using Database " + app.locals.db.databaseName);
// tell the user the URL for starting the application
console.log("URL for starting the app: http://localhost:3000");
// catch possible errors and tell the user about them:
} catch (error) {
// for using docker:
// try to connect to mongodb on mongodbservice:27017
try {
// await blocks and waits for connection, because here synchronous execution is desired
app.locals.dbConnection = await mongodb.MongoClient.connect(
// connectionString / connection URL: docker container "mongodbservice"
"mongodb://mongodbservice:" + config.mongodb.port,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
);
// connect to and use database config.mongodb.db_name (create this database, if it does not exist)
app.locals.db = await app.locals.dbConnection.db(config.mongodb.db_name);
// tell the user that the connection is established and database config.mongodb.db_name will be used for following operations
console.log("Using Database " + app.locals.db.databaseName);
// tell the user the URL for starting the application
console.log("URL for starting the app: http://<yourIP>:3000");
// if it is not possible to connect on localhost:27017 or mongodbservice:27017,
// catch possible errors and print them in the console:
} catch (error) {
console.log(error.message);
console.dir(error);
// retry until db-server is up
setTimeout(connectMongoDb, 3000);
}
}
}
)();
}
// call function above to connect to MongoDB
connectMongoDb();
// ********************************** JSNLog ***********************************
// "ensure that the JSON objects received from the client get parsed correctly"
app.use(bodyParser.json());
// jsnlog.js on the client-side sends log messages to /jsnlog.logger, using POST
app.post("/jsnlog.logger", function (req, res) {
jsnlog_nodejs(JL, req.body);
// jsnlog on the client-side does not use the response from server, therefore send an empty response
res.send('');
});
// *****************************************************************************
// router
app.use('/', indexRouter);
app.use('/api/v1/warnings', warningsRouter);
app.use('/api/v1/radar', radarRouter);
app.use('/api/v1/twitter', twitterRouter);
app.use('/api/v1/config', configRouter);
app.use('/api/v1/previousWeather', animationRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.status(404).send({err_msg: "Not Found"});
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
// respond with html page
res.send({
err_msg: err.message
});
});
module.exports = app;