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

project-17-express-api #522

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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 @@ -10,4 +10,4 @@ Describe how you approached to problem, and what tools and techniques you used t

## View it live

Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about.
https://project-17-express-api.onrender.com/
273 changes: 273 additions & 0 deletions data/animals.json
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
}
]

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"cors": "^2.8.5",
"express": "^4.17.3",
"express": "^4.21.1",
"nodemon": "^3.0.1"
}
},
"main": "server.js",
"keywords": []
}
64 changes: 54 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
import express from "express";
import cors from "cors";
import animals from "./data/animals.json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon 👀



// 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();

// 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!");
res.send("The animals of the world!");
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to list your endpoints 👀 Have a look at the instructions 😇


// 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" });
}

});
Comment on lines +46 to +59
Copy link
Contributor

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



// 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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}`);
});