From 21318aabc9da1b33e5b27293f4254c519263e32c Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 2 Feb 2021 14:10:51 -0800 Subject: [PATCH 1/3] Added routing for pets and pets/ID --- routes/pets.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/routes/pets.js b/routes/pets.js index 935c3d3..e7b8356 100644 --- a/routes/pets.js +++ b/routes/pets.js @@ -1,4 +1,32 @@ const express = require('express'); const router = express.Router(); +const petData = require('../data/pets.json'); + +// get all pets +router.get('/', function(req, res, next){ + res.json(petData); +}); + + +// get pets with specified ID +router.get('/:ID', function(req, res, next){ + let id = Number(req.params.ID); + let n = petData.length; + let output = {}; + + console.log(typeof(id)); + console.log(typeof(petData[1].id)); + + for(let i=0; i Date: Tue, 2 Feb 2021 14:17:16 -0800 Subject: [PATCH 2/3] fixed issue, wasn't return JSON object for IDs --- routes/pets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/pets.js b/routes/pets.js index e7b8356..722302d 100644 --- a/routes/pets.js +++ b/routes/pets.js @@ -24,7 +24,7 @@ router.get('/:ID', function(req, res, next){ } } - res.send(output); + res.json(output); }); From 2d3f0b8d38a5f29c512c1936b96b8ca17d3af8b6 Mon Sep 17 00:00:00 2001 From: Hanna-N9 Date: Tue, 2 Feb 2021 22:51:34 -0800 Subject: [PATCH 3/3] Adding to route cars.js --- routes/cars.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/routes/cars.js b/routes/cars.js index 935c3d3..f7ee842 100644 --- a/routes/cars.js +++ b/routes/cars.js @@ -1,4 +1,28 @@ const express = require('express'); const router = express.Router(); +//Getting information from data/cars.json +const cars = require('../data/cars.json'); + +//Uses res.json to send json and returns it as json +router.get('/', function(req, res, next) { + res.json(cars); //as json array hence res.json + }); + +//GET request that responds with single id entry. +router.get('/:id', function(req, res) { //Get route + let reqID = req.params.id; + const check = cars.find(cars => cars.id == reqID); + + //Checking the id along with if blank + if(!check) { + res.status(200).json({}); + } + else{ + res.send(check); + } + + }); + + module.exports = router; \ No newline at end of file