georgeB96

JS with mongoose

Nov 3rd, 2021
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //declarations and imports
  2. const express = require('express')
  3. const bodyParser = require('body-parser')
  4. const app = express()
  5. const port = 3000
  6. const Dog = require('./dog.js')
  7. const mongoose = require('mongoose')
  8. const {Schema} = mongoose;
  9.  
  10. const dogSchema = new Schema({
  11.   _age : Number, // Decimal128 // String
  12.   _name: String,
  13.   _breed: String,
  14.   _isNeutred: Boolean
  15. })
  16.  
  17.  
  18.  
  19.  
  20. // const path = require('path');
  21.  
  22. //app plugins or libraries
  23. app.use(bodyParser.urlencoded({extended:false}))
  24.  
  25. let dog1 = new Dog(5, "Roger", "Rotwiler");
  26. let dog2 = new Dog(5, "Boris", "Rotwiler");
  27.  
  28. let dogs = [dog1, dog2]
  29.  
  30. // console.log(dog1);
  31. app.get('/', (req, res) => {
  32.   res.send('George Blanaru')
  33. })
  34.  
  35. app.get('/message', (req, res) => {
  36.     res.send('Hi, this is a nice message');
  37. })
  38.  
  39. app.get('/othermessage', (req, res) => {
  40.     res.send('This is the second message');
  41. })
  42.  
  43. app.get('/showdog', (req, res) =>{
  44.     console.log('Someone is requiring a dog;')
  45.     res.send(dogs)
  46. })
  47.  
  48. app.post('/showdog', (req, res) =>{
  49.   //insert using post!
  50.     console.log('Someone is trying to post something');
  51.     res.send('Congrats, you posted something');
  52.     console.log(req.body);
  53.     // let name = req.body.name;
  54.     // let age = parseInt(req.body.age);
  55.     // let breed = req.body.breed;
  56.     let isNeutred = (req.body.isNeutred === 'true');
  57.     let dog = new Dog(parseInt(req.body.age),req.body.name,req.body.breed, isNeutred);
  58.     dogs.push(dog)
  59.     console.log(dog)
  60. })
  61.  
  62.  
  63.  
  64. app.delete('/deletedog/:name', (req, res) =>{
  65.   //"dogs" is the database
  66.   //newDogsArray is a holder for the updated Database
  67.   let newDogsArray =[];
  68.   //for loop in JavaScript, takes each element and assigns it to the variable "d"
  69.   //
  70.   dogs = dogs.forEach(d =>{
  71.     //checks to see if the name from the parameter matches the dogs name
  72.     if(!(d._name === req.params.name)){
  73.       //if it doesn't match, add it to the new array
  74.        newDogsArray.push(d)
  75.     }
  76.   })
  77.   console.log("Dog deleted, new database looks like this")
  78.   console.log(newDogsArray);
  79.   //update the "database / dogs"
  80.   dogs = newDogsArray;
  81.   //send a result to postman/user so it doesn't get stuck on loading
  82.   res.send("Delete in progress");
  83.  
  84.  
  85. })
  86.  
  87. app.put('/updatedog/', (req,res)=>{
  88.  
  89.   //request to use for UPDATE
  90.  
  91. })
  92.  
  93.  
  94.  
  95. app.listen(port, () => {
  96.   mongoose.connect('mongodb+srv://admin:[email protected]/myFirstDatabase?retryWrites=true&w=majority').
  97.   catch(error => console.log(error));
  98.   console.log(`Example app listening at http://localhost:${port}`)
  99. })
Advertisement
Add Comment
Please, Sign In to add comment