Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated index.js #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 28 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
Expand All @@ -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);
});
Expand Down