-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (120 loc) · 4.02 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// This JavaScript code handles the serverside logic for my fictional website storing
// color data.
import path from "node:path";
import { fileURLToPath } from "node:url";
import express from "express";
import bodyParser from "body-parser";
//Begin to use Express.
const app = express();
const port = 3004;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
//Set up body-parser to handle JSON as something
let stringParser = bodyParser.text();
//The default values for the colors database. Each color in the database is encoded
//with a hex value, an alpha value, a color name, and a case-sensitive ID which is
//the same as the key the color is stored under.
let colors = {
//Red
red: {
col: "#ff0401",
alpha: 1,
name: "Red",
id: "red",
},
//Green
green: {
col: "#02ff04",
alpha: 1,
name: "Green",
id: "green",
},
//Blue
blue: {
col: "#0404ff",
alpha: 1,
name: "Blue",
id: "blue",
},
};
//This function takes in any string and determines if it is a hex code If it is, then
//the function returns true; otherwise, it returns false.
const isValidColor = (str) => {
const regex = /\#[\da-f]{6}/gi;
const arr = str.match(regex);
return arr !== null;
};
const isValidData = (obj) => {
//Test 1: the object in question has all the valid properties
if (obj.col && obj.id && obj.alpha && obj.name) {
//console.log("all properties are present");
//Test 2: the object's col property is valid.
if (isValidColor(obj.col)) {
//console.log("col property is valid");
//Test 3: the database does not already contain an object of the same id
//as the object in question.
if (Object.hasOwn(colors, obj.id) === false) {
return true;
}
}
}
return false;
};
//A cheaty maneuver. This code will only run on my computer, but it is worth it...
//for now.
//Handles GET requests coming in to the root by sending out the homepage.
app.get("/", (req, res) => {
res.sendFile(`${__dirname}/home.html`);
});
//Handles GET requests coming in to the /colors path by sending the entire colors
//database.
app.get("/colors", (req, res) => {
res.send(colors);
});
//Handles GET requests coming in to a specific subset of the /colors path by sending
//the requested color from the database, or simply returning a 404 error if the color
//does not exist.
app.get("/colors/:color", (req, res) => {
let color = colors[req.params.color];
if (color) {
res.send(color);
} else {
res.status(404).send(
'This color does not exist.</br><a href="\\">Go back to home</a>'
);
}
});
//Once upon a time, this code to handle POST requests coming in to the /colors path
//might have seen use. However, with an easy-to-use form handling the addition of new
//colors, this code is now unneeded.
// app.post("/colors", (req, res) => {
// console.log(req);
// });
//Handles GET requests coming in to the /addcolor path. The form for adding new colors
//to the database is contained in the addcolor.html file.
app.get("/addcolor", (req, res) => {
res.sendFile(`${__dirname}/addcolor.html`);
});
//Handles POST requests coming in to the /addcolor path. Most of these requests will
//come from the form at the same location; however, there is an off-chance that there
//will be a user who sends POST requests directly. This code will take in the JSON
//from the form, ensure that it has the same format as the colors already in the
//database, and (if it is) attempt to add it. It will send also a message
//corresponding to the successfulness of the request.
app.post("/addcolor", stringParser, (req, res) => {
let contents = JSON.parse(req.body);
if (isValidData(contents)) {
Object.defineProperty(colors, contents.id, {
value: contents,
enumerable: true,
writable: true,
});
res.send('Successfully added to database!</br><a href=\"\\\">Back to home</a>');
} else {
res.status(400).send("Invalid data. Please try again.");
}
});
//Listens for requests coming in on the localhost port of our choosing, handling them
//according to the above requirements.
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});