| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- const fs = require("node:fs");
- const express = require("express");
- const app = express();
- const port = 7788;
- const data = "./data.json";
- app.use(express.json());
- // Add Access Control Allow Origin headers AAAAAAAAAAAAAAAAAAAAAA
- app.use((req, res, next) => {
- res.setHeader("Access-Control-Allow-Origin", "*");
- res.header(
- "Access-Control-Allow-Headers",
- "Origin, X-Requested-With, Content-Type, Accept",
- );
- 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) {
- res.send(err);
- return;
- }
- res.send(data);
- });
- });
- // 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 text = "";
- if (req.body) {
- text = req.body;
- console.log("REQUESTED TEXT: " + text);
- } else {
- // returns when theres no body
- console.log("ERROR (add call): THERES NO BODY");
- res.send("ERROR: NO BODY SPECIFIED");
- return;
- }
- // read the data.json file, store contents in data parameter
- fs.readFile("data.json", "utf8", (err, data) => {
- if (err) {
- res.send(err);
- return;
- }
- // 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]);
- }
- // 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 id = null;
- if (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;
- }
- // 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;
- }
- // 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]);
- }
- // 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);
- });
- // send back id for no reason
- res.send(id);
- });
- // listen for requests
- app.listen(port, () => {
- console.log(`Example app listening on port ${port}`);
- });
|