forked from cs-4241-2023/shortstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.improved.js
More file actions
116 lines (103 loc) · 3.92 KB
/
server.improved.js
File metadata and controls
116 lines (103 loc) · 3.92 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const http = require('http');
const fs = require('fs');
const mime = require('mime');
const url = require('url');
const querystring = require('querystring');
const port = process.env.PORT || 3000;
const dir = 'public/';
const appdata = [
{ 'date': '2023-09-04', 'type': 'Cardio', 'distance': 5.0, 'time': 60, 'heartRate': 150 },
// Add more initial data as needed
];
const server = http.createServer(function (request, response) {
if (request.method === 'GET') {
handleGet(request, response);
} else if (request.method === 'POST') {
handlePost(request, response);
}
});
// Create an endpoint to serve appdata object
server.on('/get-appdata', function (request, response) {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(appdata));
});
const handleGet = function (request, response) {
const filename = dir + request.url.slice(1);
const parsedURL = url.parse(request.url);
if (request.url === '/') {
sendFile(response, 'public/index.html');
} else if (request.url === '/training-data') {
// Handle GET request for training data
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(appdata));
} else if (parsedURL.pathname === '/submit-data') {
// Parse inputs from form submission
const queryParams = querystring.parse(parsedURL.query);
const newDict = {'date': queryParams.date,
'type': queryParams.type,
'distance': parseFloat(queryParams.distance),
'time': parseInt(queryParams.time),
'heartRate': parseInt(queryParams.heartRate) };
appdata.push(newDict);
console.log("UPDATED ENTRIES: " + appdata);
sendFile(response, 'public/index.html');
} else if (parsedURL.pathname === '/update') {
// Parse inputs from form submission
const queryParams = querystring.parse(parsedURL.query);
const index = queryParams.index;
const newDict = {'date': queryParams.date,
'type': queryParams.type,
'distance': parseFloat(queryParams.distance),
'time': parseInt(queryParams.time),
'heartRate': parseInt(queryParams.heartRate) };
appdata[index] = newDict;
console.log("UPDATED ENTRIES: " + appdata);
sendFile(response, 'public/index.html');
} else if (parsedURL.pathname === '/delete') {
// Parse inputs from existing session data
const queryParams = querystring.parse(parsedURL.query);
appdata.splice(parseInt(queryParams.index),1);
console.log("POST-DELETION: " + appdata);
sendFile(response, 'public/index.html');
} else {
sendFile(response, filename);
}
};
const handlePost = function (request, response) {
let dataString = '';
console.log(request.body);
request.on('data', function (data) {
dataString += data;
});
console.log("this is handling a post request");
request.on('end', function () {
const newData = JSON.parse(dataString);
// Check if the data includes an "editIndex" property for modification
if (newData.hasOwnProperty('editIndex')) {
// Update an existing training session
const index = newData.editIndex;
appdata[index] = newData.data;
} else {
// Add the new training session data to the appdata array
appdata.push(newData);
}
// Respond with a success status
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end('Data added/updated successfully');
});
};
const sendFile = function (response, filename) {
const type = mime.getType(filename);
fs.readFile(filename, function (err, content) {
if (err === null) {
response.writeHead(200, { 'Content-Type': type });
response.end(content);
} else {
response.writeHead(404);
response.end('404 Error: File Not Found');
}
});
};
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});