-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
186 lines (158 loc) · 6.34 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
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
// lib dependencies ===========================================================
var express = require('express');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var expressListRoutes = require('express-list-routes');
var timers = require('timers');
var CronJob = require('cron').CronJob;
var findRemove = require('find-remove');
// configuration ==============================================================
var app = express();
var router = express.Router();
var config = require('./config/config.js');
var port = process.env.PORT || config.port;
var ui = require('./ui_routes.js');
var ev = require('express-validation');
var server; // the server instance
var startServer = function(done) {
require('./config/passport')(passport); // pass passport for configuration
var deleteFiles = function() {
// Delete all files older than one day
var s = 24 * 60 * 60;
var result = findRemove('tmp/', {age: {seconds: s}, files: '*.*'});
//console.log('Deleted ' + Object.keys(result).length + ' files from tmp/');
};
// set up our express application
if (app.get('env') !== 'test') {
// log every request to the console
app.use(morgan('dev'));
// Call one, the server starts
deleteFiles();
// Run as cronjob
// Should not be used within the tests, otherwise they neve stop
new CronJob('0 0 0 * * *', deleteFiles, null, true, 'Europe/Berlin');
}
app.use(ui.asset('/tmp'), express.static('tmp'));
app.use(ui.asset('/static'), express.static('public'));
app.use(ui.asset('/uploads'), express.static('uploads'));
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json({limit: '10mb'})); // get information from html forms
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb' }));
// required for passport
app.use(session({ secret: 'organicityScenarioTool' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// routes =====================================================================
var routes_scenarios = require('./routes/api/v1/scenarios.js')(router, passport);
var routes_ratings = require('./routes/api/v1/ratings.js')(router, passport);
var routes_feedback = require('./routes/api/v1/feedback.js')(router, passport);
var routes_reports = require('./routes/api/v1/reports.js')(router, passport);
var routes_auth = require('./routes/api/v1/auth.js')(router, passport);
var routes_users = require('./routes/api/v1/users.js')(router, passport);
var routes_error = require('./routes/api/v1/error.js')(router, passport);
var routes_sysinfo = require('./routes/api/v1/sysinfo.js')(router, passport);
var routes_questionnaire = require('./routes/api/v1/questionnaire.js')(router, passport);
var routes_evaluations = require('./routes/api/v1/evaluations.js')(router, passport);
var routes_counter = require('./routes/api/v1/counter.js')(router, passport);
var routes_upload = require('./routes/api/v1/upload.js')(router, passport);
var routes_contactUs = require('./routes/api/v1/contact.js')(router, passport);
var routes_statistic = require('./routes/api/v1/statistic.js')(router, passport);
app.use(routes_scenarios);
app.use(routes_reports);
app.use(routes_ratings);
app.use(routes_feedback);
app.use(routes_statistic);
app.use(routes_auth);
app.use(routes_users);
app.use(routes_error);
app.use(routes_questionnaire);
app.use(routes_evaluations);
app.use(routes_counter);
app.use(routes_upload);
app.use(routes_contactUs);
// serve all (other) requests to single-page-app contained in index.html
var index = require('./index.js');
var contextPath = config.contextPath;
contextPath = contextPath.substr(0, 1) === '/' ? contextPath : '/' + contextPath;
contextPath = contextPath.substr(contextPath.length - 1) === '/' ? contextPath + '?' : contextPath + '/?';
var catchAllRoute = contextPath + '*';
app.get(catchAllRoute, function(req, res) {
res.status(200).send(index);
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
console.log('404 ', req.path);
});
// error handlers
if (app.get('env') === 'development') {
console.log('Starting server in development (or test) mode');
// development error handler
// will print stacktrace
app.use(function(err, req, res, next) {
console.log('Error', err);
// specific for validation errors
if (err instanceof ev.ValidationError) {
console.log('ValidationError');
return res.status(err.status).json(err);
}
if (!err.status || err.status === 500) {
console.log(err.stack);
}
res.status(err.status || 500);
res.format({
'application/json': function() {
res.json(err);
},
'default': function() {
res.status(406).send('Not Acceptable');
}
});
});
} else {
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.format({
'application/json': function() {
res.json({});
},
'default': function() {
res.send(406, 'Not Acceptable');
}
});
});
}
// launch =====================================================================
server = app.listen(port, function() {
if (app.get('env') !== 'test') {
console.log('Server started on ' + config.host + ':' + port + config.contextPath);
}
if (done) {
done();
}
});
return server;
};
var stopServer = function(done) {
if (server) {
if (app.get('env') !== 'test') {
console.log('Stopping server');
}
server.close();
server = null;
}
done();
};
module.exports = {
start : startServer,
stop : stopServer
};