forked from kzheng1111/online-test-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (50 loc) · 2.05 KB
/
index.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
'use strict';
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var path = require('path');
var server = require('http').Server(app);
var io = require('socket.io')(server);
var mongoose = require('mongoose');
var Enum = require('./data/enum.js');
var utils = require('./data/utils.js');
var socketUtils = require('./socket/socketUtils.js');
var answerRouter = require('./router/answerRouter.js');
var studentRouter = require('./router/studentRouter.js');
var sessionRouter = require('./router/sessionRouter.js');
var hintRouter = require('./router/hintRouter.js');
var spreadSheetRouter = require('./router/spreadSheetRouter.js');
var questionRouter = require('./router/questionRouter.js');
var dbUrl = 'mongodb://Amped:[email protected]:27017,cluster0-shard-00-01-sr2mn.mongodb.net:27017,cluster0-shard-00-02-sr2mn.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority';
var port = process.env.PORT || 8888;
//bodyParser set up for json object return
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//static folder set up
app.use(express.static(path.join(__dirname, './webapp'), {
setHeaders: function (res, path) {
if (path.includes('asset')) {
res.set('cache-control', 'public, max-age=86400');
}
}
}));
mongoose.connect(dbUrl, {useNewUrlParser: true}, function(err){
if (err) {
console.log(err);
console.log('Connection to Database failed');
process.exit(1);
} else {
console.log('Database Connected');
studentRouter.setUpAdmin();
socketUtils.setUpSocket(io);
}
});
app.use('/api', [answerRouter, studentRouter, sessionRouter, spreadSheetRouter, questionRouter, hintRouter]);
app.get('/favicon.ico', function(req, res){
res.send('Currently no favicon');
});
app.use(function(req, res){
res.sendFile('app.html', { root : __dirname + '/webapp' });
});
server.listen(port);
console.log('Server Started, listening on port: ' + port);