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

fixing errors #6

Open
wants to merge 7 commits into
base: main
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The GET route for all pets is currently "under construction". Navigate to `index

### Problem 2: GET pets by name

The GET route for pets by name is currently "under construction". Navigate to `index.js` and find the GET method at `/api/v1/pets/:name` and write the code to get a pet by name from the database.
The GET route for pets by name is currently "under construTction". Navigate to `index.js` and find the GET method at `/api/v1/pets/:name` and write the code to get a pet by name from the database.

### Problem 3: GET pet by owner's name with a query string

Expand Down
32 changes: 24 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// import the pets array from data.js
//import the pets array from data.js
const pets = require('./data');
console.log(pets);

// init express app
const express = require('express');
const app = express();
const path = require('path');

const PORT = 8080;

// GET - / - returns homepage
app.get('/', (req, res) => {
// serve up the public folder as static index.html file
//use path module to construct the absolute path to the html file
const filePath = path.join(__dirname, 'public', 'index.html');
//send the HTML file as a response
res.sendFile(filePath);

});

Expand All @@ -21,33 +27,43 @@ 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
res.json(allPets);

});

// get pet by owner with query string
app.get('/api/v1/pets/owner', (req, res) => {
// get the owner from the request

const owner = req.query.owner;

// find the pet in the pets array
const pet = pets.find(pet => pet.owner === owner);

// send the pet as a response

if (pet) {
res.json(pet);
} else {
res.status(Error).json({ message: 'Pet not found' })
}
});

// get pet by name
app.get('/api/v1/pets/:name', (req, res) => {
// get the name from the request

const petName = req.params.name;

// find the pet in the pets array
const pet = pets.find(pet => pet.name === name);
const petByName = pets.find(pet => pet.name === petName);
if (petByName) {
res.json(petByName);
} else {
res.status(404).json({error: 'Pet not found.'});
}
});


// send the pet as a response

});

app.listen(PORT, () => {
console.log('Server is listening on port ' + PORT);
});
Expand Down
Loading