|
|
@@ -6,7 +6,7 @@ const data = "./data.json";
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
-// Add Access Control Allow Origin headers
|
|
|
+// Add Access Control Allow Origin headers AAAAAAAAAAAAAAAAAAAAAA
|
|
|
app.use((req, res, next) => {
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
res.header(
|
|
|
@@ -16,6 +16,7 @@ app.use((req, res, next) => {
|
|
|
next();
|
|
|
});
|
|
|
|
|
|
+// Returns the contents of 'data.json' when client makes a GET req @ /api/text
|
|
|
app.get("/api/text", (req, res) => {
|
|
|
fs.readFile("data.json", "utf8", (err, data) => {
|
|
|
if (err) {
|
|
|
@@ -26,73 +27,97 @@ app.get("/api/text", (req, res) => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+// Accepts a JSON array of utf-8 character codes, and adds it to the array in data.json
|
|
|
app.post("/api/add", (req, res) => {
|
|
|
- let content = {};
|
|
|
+ let text = "";
|
|
|
if (req.body) {
|
|
|
- content = req.body;
|
|
|
+ text = req.body;
|
|
|
+ console.log("REQUESTED TEXT: " + text);
|
|
|
} else {
|
|
|
- console.log("AAAA");
|
|
|
+ // returns when theres no body
|
|
|
+ console.log("ERROR (add call): THERES NO BODY");
|
|
|
res.send("ERROR: NO BODY SPECIFIED");
|
|
|
return;
|
|
|
}
|
|
|
- let text = content.text;
|
|
|
|
|
|
+ // read the data.json file, store contents in data parameter
|
|
|
fs.readFile("data.json", "utf8", (err, data) => {
|
|
|
if (err) {
|
|
|
res.send(err);
|
|
|
return;
|
|
|
}
|
|
|
- let array = JSON.parse(data)["text-array"];
|
|
|
- array.push(text);
|
|
|
- let arrjson = '{"text-array": [';
|
|
|
- for (let i in array) {
|
|
|
- if (i == array.length - 1) {
|
|
|
- arrjson += '"' + array[i] + '"';
|
|
|
- } else {
|
|
|
- arrjson += '"' + array[i] + '",';
|
|
|
- }
|
|
|
+
|
|
|
+ // convert the raw json into a JS object
|
|
|
+ let storedArray = JSON.parse(data);
|
|
|
+ console.log("JSON ARRAY READ FROM data.json: ", storedArray);
|
|
|
+
|
|
|
+ let newArray = [];
|
|
|
+ // add each element of the read data and add it to newArray
|
|
|
+ for (let i in storedArray) {
|
|
|
+ newArray.push(storedArray[i]);
|
|
|
}
|
|
|
- arrjson += "]}";
|
|
|
|
|
|
+ // add the requested text
|
|
|
+ newArray.push(text);
|
|
|
+ console.log("JS ARRAY AFTER CHANGES: ", newArray);
|
|
|
+
|
|
|
+ let arrjson = JSON.stringify(newArray);
|
|
|
+ console.log("JSON ARRAY TO WRITE: ", arrjson);
|
|
|
+
|
|
|
+ // write the JSON array to data.json
|
|
|
fs.writeFileSync("data.json", arrjson);
|
|
|
|
|
|
+ // send back the input (for no reason)
|
|
|
res.send(text);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
app.post("/api/remove", (req, res) => {
|
|
|
- let content = {};
|
|
|
+ let id = null;
|
|
|
if (req.body) {
|
|
|
- content = req.body;
|
|
|
+ id = req.body;
|
|
|
+ console.log("REQUESTBODY: ", req.body);
|
|
|
} else {
|
|
|
+ // return when theres no body
|
|
|
+ console.log("ERROR (remove call): THERES NO BODY");
|
|
|
res.send("ERROR: NO BODY SPECIFIED");
|
|
|
return;
|
|
|
}
|
|
|
- let id = content.id;
|
|
|
- console.log(id);
|
|
|
+ // the body looks like: { "id": 3 }, so we need to convert it to a number
|
|
|
+ id = id["id"];
|
|
|
+ console.log("REQUESTED ID: ", id);
|
|
|
|
|
|
+ // read the data from json file
|
|
|
fs.readFile("data.json", "utf8", (err, data) => {
|
|
|
if (err) {
|
|
|
res.send(err);
|
|
|
return;
|
|
|
}
|
|
|
- let array = JSON.parse(data)["text-array"];
|
|
|
- array.splice(id, 1);
|
|
|
- let arrjson = '{"text-array": [';
|
|
|
- for (let i in array) {
|
|
|
- if (i == array.length - 1) {
|
|
|
- arrjson += '"' + array[i] + '"';
|
|
|
- } else {
|
|
|
- arrjson += '"' + array[i] + '",';
|
|
|
- }
|
|
|
+
|
|
|
+ // convert raw JSON data into a JS object
|
|
|
+ let storedArray = JSON.parse(data);
|
|
|
+ console.log("JSON ARRAY READ FROM data.json: ", storedArray);
|
|
|
+
|
|
|
+ // add each element of the read data and add it to newArray
|
|
|
+ let newArray = [];
|
|
|
+ for (let i in storedArray) {
|
|
|
+ newArray.push(storedArray[i]);
|
|
|
}
|
|
|
- arrjson += "]}";
|
|
|
+
|
|
|
+ // remove the requested ID
|
|
|
+ newArray.splice(id, 1);
|
|
|
+ console.log("JS ARRAY AFTER CHANGES: ", newArray);
|
|
|
+
|
|
|
+ let arrjson = JSON.stringify(newArray);
|
|
|
+ console.log("JSON ARRAY TO WRITE: ", arrjson);
|
|
|
+
|
|
|
fs.writeFileSync("data.json", arrjson);
|
|
|
});
|
|
|
-
|
|
|
- res.send("Removed element at id: " + id);
|
|
|
+ // send back id for no reason
|
|
|
+ res.send(id);
|
|
|
});
|
|
|
|
|
|
+// listen for requests
|
|
|
app.listen(port, () => {
|
|
|
console.log(`Example app listening on port ${port}`);
|
|
|
});
|