Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //declarations and imports
- const express = require('express')
- const bodyParser = require('body-parser')
- const app = express()
- const port = 3000
- // const Dog = require('./dog.js')
- const mongoose = require('mongoose')
- const {Schema} = mongoose;
- //schema or the set of rules for our class
- const dogSchema = new Schema({
- age : Number, // Decimal128 // String
- name: String,
- breed: String,
- isNeutred: Boolean
- })
- //constructor for our class
- const Dog = mongoose.model('Dog', dogSchema);
- //defining one object using our new constructor
- const dog = new Dog({age:5, name: "Boris", breed: "Golden Retriever", isNeutred: false})
- // const path = require('path');
- //app plugins or libraries
- app.use(bodyParser.urlencoded({extended:false}))
- // let dog1 = new Dog(5, "Roger", "Rotwiler", false);
- // let dog2 = new Dog(5, "Boris", "Rotwiler", true);
- // let dogs = [dog1, dog2]
- // console.log(dog1);
- app.get('/', (req, res) => {
- res.send('George Blanaru')
- })
- app.get('/message', (req, res) => {
- res.send('Hi, this is a nice message');
- })
- app.get('/othermessage', (req, res) => {
- res.send('This is the second message');
- })
- app.get('/showdog', (req, res) =>{
- console.log('Someone is requiring a dog;')
- res.send(dogs)
- })
- app.post('/showdog', (req, res) =>{
- //insert using post!
- console.log('Someone is trying to post something');
- res.send('Congrats, you posted something');
- console.log(req.body);
- // let name = req.body.name;
- // let age = parseInt(req.body.age);
- // let breed = req.body.breed;
- let isNeutred = (req.body.isNeutred === 'true');
- let dog = new Dog(parseInt(req.body.age),req.body.name,req.body.breed, isNeutred);
- dogs.push(dog)
- console.log(dog)
- })
- app.delete('/deletedog/:name', (req, res) =>{
- //"dogs" is the database
- //newDogsArray is a holder for the updated Database
- let newDogsArray =[];
- //for loop in JavaScript, takes each element and assigns it to the variable "d"
- //
- dogs = dogs.forEach(d =>{
- //checks to see if the name from the parameter matches the dogs name
- if(!(d._name === req.params.name)){
- //if it doesn't match, add it to the new array
- newDogsArray.push(d)
- }
- })
- console.log("Dog deleted, new database looks like this")
- console.log(newDogsArray);
- //update the "database / dogs"
- dogs = newDogsArray;
- //send a result to postman/user so it doesn't get stuck on loading
- res.send("Delete in progress");
- })
- app.put('/updatedog/', (req,res)=>{
- //request to use for UPDATE
- })
- //insert a dog into the database
- app.post('/dog',(req,res)=>{
- console.log("Inserting a dog in the database")
- dog.save()
- res.send("Dog saved")
- })
- app.listen(port, () => {
- mongoose.connect('mongodb+srv://admin:[email protected]/myFirstDatabase?retryWrites=true&w=majority').
- catch(error => console.log(error));
- console.log(`Example app listening at http://localhost:${port}`)
- })
Advertisement
Add Comment
Please, Sign In to add comment