diff --git a/index.js b/index.js index 2f2ba52..6b4fcbb 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,9 @@ const express = require('express'); const app = express(); const PORT = 8080; +// app.listen(8080, () => { +// console.log("Server is alive on port 8080") +// }) // GET - / - returns homepage app.get('/', (req, res) => { @@ -21,13 +24,14 @@ app.get('/api', (req, res) => { // get all pets from the database app.get('/api/v1/pets', (req, res) => { // send the pets array as a response - -}); + const petNames = pets.map(pet => pet.name); + res.send(`All pet names: ${petNames.join(', ')}`);}); // get pet by owner with query string app.get('/api/v1/pets/owner', (req, res) => { // get the owner from the request - + const ownerNames = pets.map(pet => pet.owner); + res.send(`All owners names: ${ownerNames.join(', ')}`);; // find the pet in the pets array const pet = pets.find(pet => pet.owner === owner); @@ -38,16 +42,34 @@ app.get('/api/v1/pets/owner', (req, res) => { // get pet by name app.get('/api/v1/pets/:name', (req, res) => { - // get the name from the request +// // get the name from the request +const name = req.params.name; - - // find the pet in the pets array +// // find the pet in the pets array const pet = pets.find(pet => pet.name === name); // send the pet as a response + if (pet) { + res.json(pet); + } else { + res.status(404).send('Pet not found'); + } }); +// from 62-71 uses query, but it doesn't make as much sence as params for me so I am sticking with it + +// app.get('/api/v1/pets', (req, res) => { +// const name = req.query.name; +// const pet = pets.find(pet => pet.name === name); + +// if (pet) { +// res.json(pet); +// } else { +// res.status(404).send('Pet not found'); +// } +// }); + app.listen(PORT, () => { console.log('Server is listening on port ' + PORT); });