index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const fs = require("node:fs");
  2. const express = require("express");
  3. const app = express();
  4. const port = 7788;
  5. const data = "./data.json";
  6. app.use(express.json());
  7. // Add Access Control Allow Origin headers AAAAAAAAAAAAAAAAAAAAAA
  8. app.use((req, res, next) => {
  9. res.setHeader("Access-Control-Allow-Origin", "*");
  10. res.header(
  11. "Access-Control-Allow-Headers",
  12. "Origin, X-Requested-With, Content-Type, Accept",
  13. );
  14. next();
  15. });
  16. // Returns the contents of 'data.json' when client makes a GET req @ /api/text
  17. app.get("/api/text", (req, res) => {
  18. fs.readFile("data.json", "utf8", (err, data) => {
  19. if (err) {
  20. res.send(err);
  21. return;
  22. }
  23. res.send(data);
  24. });
  25. });
  26. // Accepts a JSON array of utf-8 character codes, and adds it to the array in data.json
  27. app.post("/api/add", (req, res) => {
  28. let text = "";
  29. if (req.body) {
  30. text = req.body;
  31. console.log("REQUESTED TEXT: " + text);
  32. } else {
  33. // returns when theres no body
  34. console.log("ERROR (add call): THERES NO BODY");
  35. res.send("ERROR: NO BODY SPECIFIED");
  36. return;
  37. }
  38. // read the data.json file, store contents in data parameter
  39. fs.readFile("data.json", "utf8", (err, data) => {
  40. if (err) {
  41. res.send(err);
  42. return;
  43. }
  44. // convert the raw json into a JS object
  45. let storedArray = JSON.parse(data);
  46. console.log("JSON ARRAY READ FROM data.json: ", storedArray);
  47. let newArray = [];
  48. // add each element of the read data and add it to newArray
  49. for (let i in storedArray) {
  50. newArray.push(storedArray[i]);
  51. }
  52. // add the requested text
  53. newArray.push(text);
  54. console.log("JS ARRAY AFTER CHANGES: ", newArray);
  55. let arrjson = JSON.stringify(newArray);
  56. console.log("JSON ARRAY TO WRITE: ", arrjson);
  57. // write the JSON array to data.json
  58. fs.writeFileSync("data.json", arrjson);
  59. // send back the input (for no reason)
  60. res.send(text);
  61. });
  62. });
  63. app.post("/api/remove", (req, res) => {
  64. let id = null;
  65. if (req.body) {
  66. id = req.body;
  67. console.log("REQUESTBODY: ", req.body);
  68. } else {
  69. // return when theres no body
  70. console.log("ERROR (remove call): THERES NO BODY");
  71. res.send("ERROR: NO BODY SPECIFIED");
  72. return;
  73. }
  74. // the body looks like: { "id": 3 }, so we need to convert it to a number
  75. id = id["id"];
  76. console.log("REQUESTED ID: ", id);
  77. // read the data from json file
  78. fs.readFile("data.json", "utf8", (err, data) => {
  79. if (err) {
  80. res.send(err);
  81. return;
  82. }
  83. // convert raw JSON data into a JS object
  84. let storedArray = JSON.parse(data);
  85. console.log("JSON ARRAY READ FROM data.json: ", storedArray);
  86. // add each element of the read data and add it to newArray
  87. let newArray = [];
  88. for (let i in storedArray) {
  89. newArray.push(storedArray[i]);
  90. }
  91. // remove the requested ID
  92. newArray.splice(id, 1);
  93. console.log("JS ARRAY AFTER CHANGES: ", newArray);
  94. let arrjson = JSON.stringify(newArray);
  95. console.log("JSON ARRAY TO WRITE: ", arrjson);
  96. fs.writeFileSync("data.json", arrjson);
  97. });
  98. // send back id for no reason
  99. res.send(id);
  100. });
  101. // listen for requests
  102. app.listen(port, () => {
  103. console.log(`Example app listening on port ${port}`);
  104. });