Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Added changes to route cars.js #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions routes/cars.js
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 28 additions & 0 deletions routes/pets.js
Original file line number Diff line number Diff line change
@@ -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<n; i++){
let arrayID = petData[i].id;
if (arrayID === id){
output = petData[i];
}
}

res.json(output);

});


module.exports = router;