generated from Technigo/express-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 529
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
project-17-express-api #522
Draft
erikamolsson
wants to merge
6
commits into
Technigo:master
Choose a base branch
from
erikamolsson:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b7eb135
update array and two routes
7dff80f
update: animals region - not working yet
50e9d84
update: region working and find animal & its trait
a10f445
readme file uodated
1aa56d3
update: endpoints in monepage
fa33f38
missing semicolon..
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
[ | ||
{ | ||
"name": "Dog", | ||
"diet": "Omnivore", | ||
"lifespan": "10-13 years", | ||
"habitat": "Land", | ||
"trait": "Loyal", | ||
"region": "Worldwide", | ||
"id": 1 | ||
}, | ||
{ | ||
"name": "Cat", | ||
"diet": "Carnivore", | ||
"lifespan": "12-16 years", | ||
"habitat": "Land", | ||
"trait": "Independent", | ||
"region": "Worldwide", | ||
"id": 2 | ||
}, | ||
{ | ||
"name": "Horse", | ||
"diet": "Herbivore", | ||
"lifespan": "25-30 years", | ||
"habitat": "Land", | ||
"trait": "Strong and fast", | ||
"region": "Worldwide", | ||
"id": 3 | ||
}, | ||
{ | ||
"name": "Cow", | ||
"diet": "Herbivore", | ||
"lifespan": "15-20 years", | ||
"habitat": "Land", | ||
"trait": "Produces milk", | ||
"region": "Worldwide", | ||
"id": 4 | ||
}, | ||
{ | ||
"name": "Chicken", | ||
"diet": "Omnivore", | ||
"lifespan": "5-10 years", | ||
"habitat": "Land", | ||
"trait": "Lays eggs", | ||
"region": "Worldwide", | ||
"id": 5 | ||
}, | ||
{ | ||
"name": "Goat", | ||
"diet": "Herbivore", | ||
"lifespan": "10-15 years", | ||
"habitat": "Land", | ||
"trait": "Climbs well", | ||
"region": "Worldwide", | ||
"id": 6 | ||
}, | ||
{ | ||
"name": "Sheep", | ||
"diet": "Herbivore", | ||
"lifespan": "10-12 years", | ||
"habitat": "Land", | ||
"trait": "Provides wool", | ||
"region": "Worldwide", | ||
"id": 7 | ||
}, | ||
{ | ||
"name": "Pig", | ||
"diet": "Omnivore", | ||
"lifespan": "10-15 years", | ||
"habitat": "Land", | ||
"trait": "Intelligent", | ||
"region": "Worldwide", | ||
"id": 8 | ||
}, | ||
{ | ||
"name": "Duck", | ||
"diet": "Omnivore", | ||
"lifespan": "5-10 years", | ||
"habitat": "Land and water", | ||
"trait": "Webbed feet", | ||
"region": "Worldwide", | ||
"id": 9 | ||
}, | ||
{ | ||
"name": "Rabbit", | ||
"diet": "Herbivore", | ||
"lifespan": "8-12 years", | ||
"habitat": "Land", | ||
"trait": "Quick and small", | ||
"region": "Worldwide", | ||
"id": 10 | ||
}, | ||
{ | ||
"name": "Llama", | ||
"diet": "Herbivore", | ||
"lifespan": "15-25 years", | ||
"habitat": "Land", | ||
"trait": "Soft wool", | ||
"region": "South America", | ||
"id": 11 | ||
}, | ||
{ | ||
"name": "Donkey", | ||
"diet": "Herbivore", | ||
"lifespan": "25-30 years", | ||
"habitat": "Land", | ||
"trait": "Hard worker", | ||
"region": "Worldwide", | ||
"id": 12 | ||
}, | ||
{ | ||
"name": "Elephant", | ||
"diet": "Herbivore", | ||
"lifespan": "60-70 years", | ||
"habitat": "Land", | ||
"trait": "Largest land animal", | ||
"region": "Africa, Asia", | ||
"id": 13 | ||
}, | ||
{ | ||
"name": "Lion", | ||
"diet": "Carnivore", | ||
"lifespan": "10-14 years", | ||
"habitat": "Land", | ||
"trait": "King of the jungle", | ||
"region": "Africa", | ||
"id": 14 | ||
}, | ||
{ | ||
"name": "Tiger", | ||
"diet": "Carnivore", | ||
"lifespan": "10-15 years", | ||
"habitat": "Land", | ||
"trait": "Striped fur", | ||
"region": "Asia", | ||
"id": 15 | ||
}, | ||
{ | ||
"name": "Zebra", | ||
"diet": "Herbivore", | ||
"lifespan": "20-25 years", | ||
"habitat": "Land", | ||
"trait": "Striped body", | ||
"region": "Africa", | ||
"id": 16 | ||
}, | ||
{ | ||
"name": "Giraffe", | ||
"diet": "Herbivore", | ||
"lifespan": "25-30 years", | ||
"habitat": "Land", | ||
"trait": "Long neck", | ||
"region": "Africa", | ||
"id": 17 | ||
}, | ||
{ | ||
"name": "Kangaroo", | ||
"diet": "Herbivore", | ||
"lifespan": "20-23 years", | ||
"habitat": "Land", | ||
"trait": "Hops on two legs", | ||
"region": "Australia", | ||
"id": 18 | ||
}, | ||
{ | ||
"name": "Koala", | ||
"diet": "Herbivore", | ||
"lifespan": "13-18 years", | ||
"habitat": "Land", | ||
"trait": "Sleeps a lot", | ||
"region": "Australia", | ||
"id": 19 | ||
}, | ||
{ | ||
"name": "Panda", | ||
"diet": "Herbivore", | ||
"lifespan": "20-30 years", | ||
"habitat": "Land", | ||
"trait": "Loves bamboo", | ||
"region": "China", | ||
"id": 20 | ||
}, | ||
{ | ||
"name": "Chimpanzee", | ||
"diet": "Omnivore", | ||
"lifespan": "30-40 years", | ||
"habitat": "Land", | ||
"trait": "Tool user", | ||
"region": "Africa", | ||
"id": 21 | ||
}, | ||
{ | ||
"name": "Polar Bear", | ||
"diet": "Carnivore", | ||
"lifespan": "20-25 years", | ||
"habitat": "Land and ice", | ||
"trait": "Strong swimmer", | ||
"region": "Arctic", | ||
"id": 22 | ||
}, | ||
{ | ||
"name": "Grizzly Bear", | ||
"diet": "Omnivore", | ||
"lifespan": "20-25 years", | ||
"habitat": "Land", | ||
"trait": "Powerful predator", | ||
"region": "North America", | ||
"id": 23 | ||
}, | ||
{ | ||
"name": "Penguin", | ||
"diet": "Carnivore", | ||
"lifespan": "15-20 years", | ||
"habitat": "Ocean and land", | ||
"trait": "Cannot fly but swims", | ||
"region": "Antarctica", | ||
"id": 24 | ||
}, | ||
{ | ||
"name": "Dolphin", | ||
"diet": "Carnivore", | ||
"lifespan": "40-60 years", | ||
"habitat": "Ocean", | ||
"trait": "Highly intelligent", | ||
"region": "Oceans worldwide", | ||
"id": 25 | ||
}, | ||
{ | ||
"name": "Shark", | ||
"diet": "Carnivore", | ||
"lifespan": "20-30 years", | ||
"habitat": "Ocean", | ||
"trait": "Apex predator", | ||
"region": "Oceans worldwide", | ||
"id": 26 | ||
}, | ||
{ | ||
"name": "Whale", | ||
"diet": "Carnivore", | ||
"lifespan": "50-100 years", | ||
"habitat": "Ocean", | ||
"trait": "Largest animal", | ||
"region": "Oceans worldwide", | ||
"id": 27 | ||
}, | ||
{ | ||
"name": "Octopus", | ||
"diet": "Carnivore", | ||
"lifespan": "1-2 years", | ||
"habitat": "Ocean", | ||
"trait": "Eight arms", | ||
"region": "Oceans worldwide", | ||
"id": 28 | ||
}, | ||
{ | ||
"name": "Wolf", | ||
"diet": "Carnivore", | ||
"lifespan": "8-13 years", | ||
"habitat": "Land", | ||
"trait": "Pack hunter", | ||
"region": "Worldwide (non-domesticated areas)", | ||
"id": 29 | ||
}, | ||
{ | ||
"name": "Falcon", | ||
"diet": "Carnivore", | ||
"lifespan": "12-16 years", | ||
"habitat": "Sky", | ||
"trait": "Fastest bird", | ||
"region": "Worldwide", | ||
"id": 30 | ||
} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,79 @@ | ||
import express from "express"; | ||
import cors from "cors"; | ||
import animals from "./data/animals.json"; | ||
|
||
|
||
// If you're using one of our datasets, uncomment the appropriate import below | ||
// to get started! | ||
// import avocadoSalesData from "./data/avocado-sales.json"; | ||
// import booksData from "./data/books.json"; | ||
// import goldenGlobesData from "./data/golden-globes.json"; | ||
// import netflixData from "./data/netflix-titles.json"; | ||
// import topMusicData from "./data/top-music.json"; | ||
|
||
// Defines the port the app will run on. Defaults to 8080, but can be overridden | ||
// when starting the server. Example command to overwrite PORT env variable value: | ||
// PORT=9000 npm start | ||
const port = process.env.PORT || 8080; | ||
const port = process.env.PORT || 9000; | ||
const app = express(); | ||
const expressListEndpoints = require("express-list-endpoints"); | ||
|
||
// Add middlewares to enable cors and json body parsing | ||
app.use(cors()); | ||
app.use(express.json()); | ||
|
||
// Start defining your routes here | ||
// Start "page" | ||
app.get("/", (req, res) => { | ||
res.send("Hello Technigo!"); | ||
const endpoints = expressListEndpoints(app); | ||
res.json({ | ||
message: "The animals of the world!", | ||
endpoints: endpoints | ||
}); | ||
}); | ||
|
||
// Get alla animals from array | ||
app.get("/animals", (req, res) => { | ||
res.send(animals); | ||
}); | ||
|
||
|
||
// Get all animals from id | ||
app.get("/animals/:id", (req, res) => { | ||
const id = parseInt(req.params.id); | ||
const animalId = animals.find((animal) => animal.id === id); | ||
if (animalId) { | ||
res.json(animalId); // Send the specific animal | ||
} else { | ||
res.status(404).json({ error: "Animal not found" }); | ||
} | ||
}); | ||
|
||
|
||
// Get animals from specific regions | ||
app.get("/regions/:region", (req, res) => { | ||
const region = req.params.region; | ||
|
||
const specificRegion = animals.filter((animal) => | ||
animal.region.toLowerCase().includes(region) | ||
); | ||
|
||
if (specificRegion) { | ||
res.json(specificRegion); | ||
} else { | ||
res.status(404).json({ error: "Animal not found" }); | ||
} | ||
|
||
}); | ||
|
||
|
||
// Get all animals specific trait | ||
app.get("/traits/:name", (req, res) => { | ||
const name = req.params.name; // Get the name from the route | ||
const animal = animals.find((a) => a.name.toLowerCase() === name); // Find the animal by name | ||
|
||
if (animal) { | ||
res.json({ name: animal.name, trait: animal.trait }); // Respond with the name and trait | ||
} else { | ||
res.status(404).json({ error: "Animal not found" }); // Animal not found | ||
} | ||
|
||
}); | ||
Comment on lines
+63
to
73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was close to saying the same thing about this endpoint, but here you are actually returning the trait, so it's all good ⭐ |
||
|
||
// Start the server | ||
app.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to see some additional filtering! Just remember that a RESTful API names its routes after what they return (and you return animals here). Therefor, a more RESTful approach would be to include it as query params in your /animals route:
/animals?region=worldwide