-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (38 loc) · 1.27 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
const express = require("express");
// for timing algorithms
const AlgorithmTimer = require("./src/algorithm_timer");
const timer = new AlgorithmTimer;
// for timing custom algorithms, avoiding global prototype editing
const CustomAlgorithmTimer = require("./src/custom_algorithm_timer");
const customTimer = new CustomAlgorithmTimer;
const app = express();
// serves static files from the public folder
app.use(express.static('public'));
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css'));
app.use(express.json());
// retrieves the homepage html
app.get("/", (req, res) => {
res.sendFile(__dirname + '/public/base.html');
});
// post method sends selection to server side and returns json of timings
app.post('/api', (req, res) => {
var nameditem = req.body.value;
let data;
if(nameditem.includes('custom')){
customTimer.recordTime(String(nameditem));
data = customTimer.recordTime(String(nameditem));
} else {
// calling function first to remove abnormal 1st results
timer.recordTime(String(nameditem));
data = timer.recordTime(String(nameditem));
}
res.json({
status: 'success',
value: req.body.value,
timer: data
});
})
const port = 3000;
app.listen(port, () => {
console.log(`Server listening at ${port}`);
});