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
108 lines (89 loc) · 3.49 KB
/
server.improved.js
File metadata and controls
108 lines (89 loc) · 3.49 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
const http = require('http'),
fs = require('fs'),
// IMPORTANT: you must run `npm install` in the directory for this assignment
// to install the mime library if you're testing this on your local machine.
// However, Glitch will install it automatically by looking in your package.json
// file.
mime = require('mime'),
dir = 'public/',
port = 3000
const appdata = [
{ 'name': 'Pumpkin Patch', 'primary_color': '#606C38', 'secondary_color': '#283618', 'teritary_color': '#FEFAE0', 'accent_1': '#DDA15E', 'accent_2': '#BC6C25', 'contrast': 2.26 },
{ 'name': 'Drywall', 'primary_color': '#FBFBF2', 'secondary_color': '#E5E6E4', 'teritary_color': '#CFD2CD', 'accent_1': '#A6A2A2', 'accent_2': '#847577', 'contrast': 1.2 },
{ 'name': 'Crime Scene', 'primary_color': '#FF2E00', 'secondary_color': '#7C7C7C', 'teritary_color': '#EEE5E9', 'accent_1': '#FDE12D', 'accent_2': '#2D2327', 'contrast': 1.12 },
]
const server = http.createServer(function(request, response) {
if (request.method === 'GET') {
handleGet(request, response)
} else if (request.method === 'POST') {
handlePost(request, response)
} else if (request.method === 'DELETE') {
handleDelete(request, response);
}
})
const handleGet = function(request, response) {
const filename = dir + request.url.slice(1)
if (request.url === '/') {
sendFile(response, 'public/index.html')
} else if (request.url === '/json') {
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify(appdata));
} else {
sendFile(response, filename)
}
}
const handleDelete = function(request, response) {
let dataString = ''
request.on('data', function(data) {
dataString += data
})
request.on('end', function() {
let data = JSON.parse(dataString);
const index = appdata.findIndex(item => item.name === data["name"]);
if (index !== -1) {
appdata.splice(index, 1);
}
response.writeHead(200, "OK", { 'Content-Type': 'text/plain' })
response.end(JSON.stringify(appdata));
})
}
const handlePost = function(request, response) {
let dataString = ''
request.on('data', function(data) {
dataString += data
})
request.on('end', function() {
let data = JSON.parse(dataString);
const index = appdata.findIndex(item => item.name === data["name"]);
if (index === -1) {
appdata.push(data);
sortByContrast();
response.writeHead(200, "OK", { 'Content-Type': 'text/plain' })
response.end(JSON.stringify(appdata))
} else {
response.writeHead(400, "Bad Request", { 'Content-Type': 'text/plain' });
response.end("Name already exists");
}
})
}
const sendFile = function(response, filename) {
const type = mime.getType(filename)
fs.readFile(filename, function(err, content) {
// if the error = null, then we've loaded the file successfully
if (err === null) {
// status code: https://httpstatuses.com
response.writeHeader(200, { 'Content-Type': type })
response.end(content)
} else {
// file not found, error code 404
response.writeHeader(404)
response.end('404 Error: File Not Found')
}
})
}
function sortByContrast() {
appdata.sort(function(a, b) {
return b.contrast - a.contrast;
})
}
server.listen(process.env.PORT || port)