Advertisement
Niicksana

catshelter.js

Sep 21st, 2022
736
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 path = require('path');
  4. const qs = require('querystring');
  5. const formidable = require('formidable');
  6. const breeds = require('../data/breeds.json');
  7. const cats = require('../data/cats.json');
  8.  
  9. module.exports = (req, res) => {
  10.     const pathname = url.parse(req.url).pathname;
  11.  
  12.     if (pathname === '/cats/add-cat' && req.method === 'GET') {
  13.         let filePath = path.normalize(path.join(__filename.slice(0, __filename.length - 8), '../views/addCat.html'));
  14.  
  15.         const index = fs.createReadStream(filePath);
  16.  
  17.         index.on('data', (data) => {
  18.             let catBreedPlaceholder = breeds.map((breed) => `<option value="${breed}">${breed}</option>`);
  19.             let modifiedData = data.toString().replace('{{catBreeds}}', catBreedPlaceholder);
  20.  
  21.             res.write(modifiedData);
  22.         });
  23.  
  24.         index.on('end', () => {
  25.             res.end();
  26.         });
  27.  
  28.         index.on('error', (err) => {
  29.             console.log(err);
  30.         });
  31.     } else if (pathname === '/cats/add-breed' && req.method === 'GET') {
  32.         let filePath = path.normalize(path.join(__filename.slice(0, __filename.length - 8), '../views/addBreed.html'));
  33.  
  34.         const index = fs.createReadStream(filePath);
  35.  
  36.         index.on('data', (data) => {
  37.             res.write(data);
  38.         });
  39.  
  40.         index.on('end', () => {
  41.             res.end();
  42.         });
  43.  
  44.         index.on('error', (err) => {
  45.             console.log(err);
  46.         });
  47.     } else if (pathname === '/cats/add-cat' && req.method === 'POST') {
  48.         let form = new formidable.IncomingForm();
  49.  
  50.         form.parse(req, (err, fields, files) => {
  51.             if (err) {
  52.                 throw err;
  53.             };
  54.  
  55.             let midPath = files.upload.path;
  56.             //console.log(fields);
  57.             //console.log(files);
  58.             //console.log(midPath);
  59.             let newPath = path.normalize(path.join(globalPath, '/content/images/' + files.upload.name));
  60.  
  61.             fs.readFile('./data/cats.json', 'utf-8', (err, data) => {
  62.                 if (err) {
  63.                     throw err;
  64.                 };
  65.  
  66.                 let allCats = JSON.parse(data);
  67.                 allCats.push({ id: cats.length + 1, ...fields, image: fields.upload.name });
  68.  
  69.                 let json = JSON.stringify(allCats);
  70.                 fs.writeFile('./data/cats.json', json, () => {
  71.                     res.writeHead(302, { location: '/' });
  72.                     res.end();
  73.                 });
  74.             });
  75.         });
  76.     } else if (pathname === '/cats/add-breed' && req.method === 'POST') {
  77.         let formData = '';
  78.  
  79.         req.on('data', (data) => {
  80.             formData += data;
  81.         });
  82.  
  83.         req.on('end', () => {
  84.             let body = qs.parse(formData);
  85.  
  86.             fs.readFile('./data/breeds.json', (err, data) => {
  87.                 if (err) {
  88.                     throw err;
  89.                 }
  90.  
  91.                 let breeds = JSON.parse(data);
  92.                 breeds.push(body.breed);
  93.                 let json = JSON.stringify(breeds);
  94.  
  95.                 fs.writeFile('./data/breeds.json', json, 'utf-8', () => console.log('The breed was uploaded successfully!'));
  96.             });
  97.  
  98.             res.writeHead(302, { location: '/' });
  99.             res.end();
  100.         });
  101.     } else {
  102.         return true;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement