-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
75 lines (60 loc) · 2.59 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
require('dotenv').config({path: 'config/application.env'});
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const path = require('path');
const favicon = require('serve-favicon');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const models = require('./models'); // Register schemas
const auth = require('./middleware/jwtMiddleware');
const SocketService = require('./services/SocketService');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon(path.join(__dirname, 'public/images', 'favicon.ico')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
if (process.env.MORGAN_FORMAT) app.use(morgan(process.env.MORGAN_FORMAT));
// JWT Security
app.use(['/api/challenge*', '/api/alerts*', '/api/envBridge*'], auth.jwtAuthProtected);
app.use('/node_modules', express.static(path.join(__dirname, 'node_modules')));
app.use('/', require('./routes/EjsViewController'));
app.use('/auth', require('./routes/AuthorizationController'));
app.use('/api/player', require('./routes/PlayerController'));
app.use('/api/team', require('./routes/TeamController'));
app.use('/api/challenge/player', require('./routes/challenges/PlayerChallengeController'));
app.use('/api/challenge/team', require('./routes/challenges/TeamChallengeController'));
app.use('/api/alerts', require('./routes/AlertController'));
app.use('/api/envBridge', require('./routes/EnvironmentBridgeController'));
app.use('/manual', require('./routes/ManualTaskController'));
// Catch 404 and forward to error handler
app.use((req, res, next) => {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handler
app.use((err, req, res, next) => {
console.error(err);
res.status(err.status || 500);
res.json(err.message);
});
SocketService.init(io);
if (!process.env.DISABLE_MONGOOSE_CONNECT) {
mongoose
.connect(process.env.MONGODB_URI, {
useCreateIndex: true,
useNewUrlParser: true,
promiseLibrary: Promise
})
.catch(console.error);
} else {
console.log('Mongoose connection was blocked by DISABLE_MONGOOSE_CONNECT environment variable');
}
const listeningServer = server.listen(process.env.PORT, () => {
console.log(`Server is listening on port ${listeningServer.address().port}`);
});
module.exports = listeningServer;