Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const url = require("url");
- const fs = require("fs");
- const qs = require("querystring");
- const path = require("path");
- const formidable = require("formidable");
- const cats = require("../data/cats.json");
- module.exports = (req, res) => {
- const pathname = url.parse(req.url).pathname;
- if (pathname === "/cats/add-cat" && req.method === "GET") {
- let filePath = path.normalize(path.join(__dirname, "../views/addCat.html"));
- fs.readFile(filePath, "utf-8", (err, data) => {
- if (err) {
- console.log(err);
- res.writeHead(404, {
- "Content-Type": "text/plain",
- });
- res.write("404 not found :(");
- res.end();
- return;
- }
- fs.readFile("./data/breeds.json", "utf-8", (err, breedsData) => {
- if (err) {
- console.log(err);
- res.writeHead(500, {
- "Content-Type": "text/plain",
- });
- res.write("Internal server error");
- res.end();
- return;
- }
- const breeds = JSON.parse(breedsData);
- const breedOptions = breeds
- .map((breed) => `<option value="${breed}">${breed}</option>`)
- .join("\n");
- const finalHtml = data.replace("{{catBreeds}}", breedOptions);
- res.writeHead(200, {
- "Content-Type": "text/html",
- });
- res.write(finalHtml);
- res.end();
- });
- });
- } else if (pathname === "/cats/add-breed" && req.method === "GET") {
- let filePath = path.normalize(
- path.join(__dirname, "../views/addBreed.html")
- );
- fs.readFile(filePath, (err, data) => {
- if (err) {
- console.log(err);
- res.writeHead(404, {
- "Content-Type": "text/plain",
- });
- res.write("404 not found :(");
- res.end();
- return;
- }
- res.writeHead(200, {
- "Content-Type": "text/html",
- });
- res.write(data);
- res.end();
- });
- } else if (pathname === "/cats/add-cat" && req.method === "POST") {
- let form = new formidable.IncomingForm();
- form.uploadDir = "./content/images";
- form.keepExtensions = true;
- form.parse(req, (err, fields, files) => {
- if (err) {
- console.error(err);
- res.writeHead(500, { "Content-Type": "text/plain" });
- res.write("Error processing form data");
- res.end();
- return;
- }
- let oldPath = files.upload.filepath;
- let newPath = path.normalize(
- path.join(
- __dirname,
- "../content/images/",
- files.upload.originalFilename
- )
- );
- fs.rename(oldPath, newPath, (err) => {
- if (err) throw err;
- const newCat = {
- id: cats.length + 1,
- name: fields.name,
- description: fields.description,
- breed: fields.breed,
- image: files.upload.originalFilename,
- };
- cats.push(newCat);
- fs.writeFile(
- "./data/cats.json",
- JSON.stringify(cats, null, 2),
- "utf-8",
- (err) => {
- if (err) {
- console.error(err);
- res.writeHead(500, { "Content-Type": "text/plain" });
- res.write("Failed to save the new cat");
- res.end();
- return;
- }
- console.log("The cat was added successfully!");
- res.writeHead(302, { location: "/" });
- }
- );
- });
- });
- } else if (pathname === "/cats/add-breed" && req.method === "POST") {
- let filePath = path.normalize(path.join(__dirname, "../data/breeds.json"));
- let formData = "";
- req.on("data", (data) => {
- formData += data;
- });
- req.on("end", () => {
- let body = qs.parse(formData);
- fs.readFile(filePath, (err, data) => {
- if (err) {
- throw err;
- }
- let breeds = JSON.parse(data);
- breeds.push(body.breed);
- let json = JSON.stringify(breeds);
- fs.writeFile(filePath, json, "utf-8", () =>
- console.log("The breed was uploaded successfully!")
- );
- });
- res.writeHead(302, { location: "/" });
- res.end();
- });
- } else {
- true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment