-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
90 lines (72 loc) · 2.61 KB
/
main.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
/**
* Enable Express server routes and their handlers
*/
require('dotenv').config()
const path = require('path')
const express = require('express')
const app = express()
const lti = require('./server/routes/lti')
const users = require('./server/routes/users')
const adminRouter = require('./server/routes/adminRouter')
const scripts = require('./server/routes/scripts')
const pool = require('./util/db')
const sess = require('./util/session')
app.set('trust proxy', 1)
app.use(sess)
// Used to parse request data that sent from web pages in JSON format
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use('/users', users)
app.use('/lti', lti)
app.use('/admin', adminRouter)
app.use('/scripts', scripts)
app.post('/add-selection', (req, res) => {
console.log(req.body)
console.log(req.session)
/**
* Create new entry in 'results' table in database
* Answer_date is of type 'timestamp with time zone' in PostgreSQL.
*/
pool.query("INSERT INTO results(session_id, student_id, username, assessment, prompt_image, answer, solution, answer_date, guided) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", [
req.sessionID,
req.session.student_id,
req.session.username,
req.body.assessment,
req.body.promptImage,
req.body.answer,
req.body.solution,
req.body.answerDate,
req.body.guided
], (err, result) => {
if (err) throw err;
res.send('Selection successfully added')
})
})
// As an admin, unlock the testing section for students to take
app.post('/unlock-testing', function (req, res) {
pool.query(`UPDATE unlocksections SET unlocked=true WHERE assessment='testing'`, function (err, result) {
if (err) throw err;
})
})
// Check if admin has granted access to the testing section
app.get('/unlocked-testing', function (req, res) {
pool.query(`SELECT unlocked FROM unlocksections WHERE assessment='testing'`, function (err, result) {
console.log(result.rows[0])
res.status(200).json(result.rows[0])
})
})
// Renders HTML file from Simplephy source code
app.use(express.static(path.join(__dirname, 'static')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'static', 'main.html'))
})
app.get('/final-results', function (req, res) {
res.sendFile(path.join(__dirname, 'static', 'finalResults.html'))
})
// Renders HTML file generated by npm build command
app.use(express.static(path.join(__dirname, 'build')));
app.get(/[a-z]+/, function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
module.exports = app