-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (52 loc) · 1.84 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
const express = require('express');
const app = express();
const path = require('path');
// Serve React files
app.use(express.static(path.join(__dirname, 'client/build')));
// Configure helmet
const helmet = require('helmet');
app.use(helmet());
// Configure sanitizer
const expressSanitizer = require('express-sanitizer');
app.use(expressSanitizer());
// Configure body parsing
const bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Configure cookie parsing
const cookieParser = require('cookie-parser');
app.use(cookieParser());
// Configure form processing
const formData = require('express-form-data');
app.use(formData.parse({
uploadDir: require('os').tmpdir(),
autoClean: true
}));
app.use(formData.format());
app.use(formData.union());
// Configure passport
const passport = require('passport');
app.use(passport.initialize());
// Deploy OpenAPI
const swaggerUI = require('swagger-ui-express');
const swaggerDocument = require('yamljs').load('res/openapi.yml');
app.use("/api/docs", swaggerUI.serve, swaggerUI.setup(swaggerDocument));
// Connect to database
const mongoose = require('mongoose');
if (process.env.DATABASE_PASSWORD) {
mongoose.connect('mongodb://MattGill98:' + process.env.DATABASE_PASSWORD + '@ds111082.mlab.com:11082/dc2410');
} else {
mongoose.connect('mongodb://localhost:27017');
}
// Create tables
const User = require('./models/users.js')(mongoose);
const Event = require('./models/events.js')(mongoose);
// Add routes
app.use('/api', require('./routes/public.js')(Event));
app.use('/api', require('./routes/student.js')(Event));
app.use('/api', require('./routes/organiser.js')(Event));
module.exports = app;
// Redirect unknown requests to the frontend.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build/index.html'));
});