victoriaSD

cat shelter2.0

Sep 13th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const url = require("url");
  2. const fs = require("fs");
  3. const qs = require("querystring");
  4. const path = require("path");
  5. const formidable = require("formidable");
  6. const cats = require("../data/cats.json");
  7.  
  8. module.exports = (req, res) => {
  9.   const pathname = url.parse(req.url).pathname;
  10.  
  11.   if (pathname === "/cats/add-cat" && req.method === "GET") {
  12.     let filePath = path.normalize(path.join(__dirname, "../views/addCat.html"));
  13.  
  14.     fs.readFile(filePath, "utf-8", (err, data) => {
  15.       if (err) {
  16.         console.log(err);
  17.         res.writeHead(404, {
  18.           "Content-Type": "text/plain",
  19.         });
  20.  
  21.         res.write("404 not found :(");
  22.         res.end();
  23.         return;
  24.       }
  25.  
  26.       fs.readFile("./data/breeds.json", "utf-8", (err, breedsData) => {
  27.         if (err) {
  28.           console.log(err);
  29.           res.writeHead(500, {
  30.             "Content-Type": "text/plain",
  31.           });
  32.  
  33.           res.write("Internal server error");
  34.           res.end();
  35.           return;
  36.         }
  37.  
  38.         const breeds = JSON.parse(breedsData);
  39.         const breedOptions = breeds
  40.           .map((breed) => `<option value="${breed}">${breed}</option>`)
  41.           .join("\n");
  42.  
  43.         const finalHtml = data.replace("{{catBreeds}}", breedOptions);
  44.  
  45.         res.writeHead(200, {
  46.           "Content-Type": "text/html",
  47.         });
  48.  
  49.         res.write(finalHtml);
  50.         res.end();
  51.       });
  52.     });
  53.   } else if (pathname === "/cats/add-breed" && req.method === "GET") {
  54.     let filePath = path.normalize(
  55.       path.join(__dirname, "../views/addBreed.html")
  56.     );
  57.  
  58.     fs.readFile(filePath, (err, data) => {
  59.       if (err) {
  60.         console.log(err);
  61.         res.writeHead(404, {
  62.           "Content-Type": "text/plain",
  63.         });
  64.  
  65.         res.write("404 not found :(");
  66.         res.end();
  67.         return;
  68.       }
  69.  
  70.       res.writeHead(200, {
  71.         "Content-Type": "text/html",
  72.       });
  73.  
  74.       res.write(data);
  75.       res.end();
  76.     });
  77.   } else if (pathname === "/cats/add-cat" && req.method === "POST") {
  78.     let form = new formidable.IncomingForm();
  79.     form.uploadDir = "./content/images";
  80.     form.keepExtensions = true;
  81.  
  82.     form.parse(req, (err, fields, files) => {
  83.       if (err) {
  84.         console.error(err);
  85.         res.writeHead(500, { "Content-Type": "text/plain" });
  86.         res.write("Error processing form data");
  87.         res.end();
  88.         return;
  89.       }
  90.  
  91.       let oldPath = files.upload.filepath;
  92.       let newPath = path.normalize(
  93.         path.join(
  94.           __dirname,
  95.           "../content/images/",
  96.           files.upload.originalFilename
  97.         )
  98.       );
  99.  
  100.       fs.rename(oldPath, newPath, (err) => {
  101.         if (err) throw err;
  102.  
  103.         const newCat = {
  104.           id: cats.length + 1,
  105.           name: fields.name,
  106.           description: fields.description,
  107.           breed: fields.breed,
  108.           image: files.upload.originalFilename,
  109.         };
  110.  
  111.         cats.push(newCat);
  112.  
  113.         fs.writeFile(
  114.           "./data/cats.json",
  115.           JSON.stringify(cats, null, 2),
  116.           "utf-8",
  117.           (err) => {
  118.             if (err) {
  119.               console.error(err);
  120.               res.writeHead(500, { "Content-Type": "text/plain" });
  121.               res.write("Failed to save the new cat");
  122.               res.end();
  123.               return;
  124.             }
  125.  
  126.             console.log("The cat was added successfully!");
  127.             res.writeHead(302, { location: "/" });
  128.           }
  129.         );
  130.       });
  131.     });
  132.   } else if (pathname === "/cats/add-breed" && req.method === "POST") {
  133.     let filePath = path.normalize(path.join(__dirname, "../data/breeds.json"));
  134.     let formData = "";
  135.  
  136.     req.on("data", (data) => {
  137.       formData += data;
  138.     });
  139.  
  140.     req.on("end", () => {
  141.       let body = qs.parse(formData);
  142.  
  143.       fs.readFile(filePath, (err, data) => {
  144.         if (err) {
  145.           throw err;
  146.         }
  147.  
  148.         let breeds = JSON.parse(data);
  149.         breeds.push(body.breed);
  150.         let json = JSON.stringify(breeds);
  151.  
  152.         fs.writeFile(filePath, json, "utf-8", () =>
  153.           console.log("The breed was uploaded successfully!")
  154.         );
  155.       });
  156.  
  157.       res.writeHead(302, { location: "/" });
  158.       res.end();
  159.     });
  160.   } else {
  161.     true;
  162.   }
  163. };
  164.  
Advertisement
Add Comment
Please, Sign In to add comment