From b7eb135e5f9872716ecf686957805a3b199799be Mon Sep 17 00:00:00 2001 From: Erika Olsson <erikamolsson87@gmail.com> Date: Thu, 5 Dec 2024 19:44:05 +0100 Subject: [PATCH 1/6] update array and two routes --- data/animals.json | 273 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 6 +- server.js | 31 ++++-- 3 files changed, 298 insertions(+), 12 deletions(-) create mode 100644 data/animals.json diff --git a/data/animals.json b/data/animals.json new file mode 100644 index 000000000..b8fa8a2db --- /dev/null +++ b/data/animals.json @@ -0,0 +1,273 @@ +[ + { + "id": 1, + "name": "Dog", + "diet": "Omnivore", + "lifespan": "10-13 years", + "habitat": "Land", + "trait": "Loyal", + "region": "Worldwide" + }, + { + "id": 2, + "name": "Cat", + "diet": "Carnivore", + "lifespan": "12-16 years", + "habitat": "Land", + "trait": "Independent", + "region": "Worldwide" + }, + { + "id": 3, + "name": "Horse", + "diet": "Herbivore", + "lifespan": "25-30 years", + "habitat": "Land", + "trait": "Strong and fast", + "region": "Worldwide" + }, + { + "id": 4, + "name": "Cow", + "diet": "Herbivore", + "lifespan": "15-20 years", + "habitat": "Land", + "trait": "Produces milk", + "region": "Worldwide" + }, + { + "id": 5, + "name": "Chicken", + "diet": "Omnivore", + "lifespan": "5-10 years", + "habitat": "Land", + "trait": "Lays eggs", + "region": "Worldwide" + }, + { + "id": 6, + "name": "Goat", + "diet": "Herbivore", + "lifespan": "10-15 years", + "habitat": "Land", + "trait": "Climbs well", + "region": "Worldwide" + }, + { + "id": 7, + "name": "Sheep", + "diet": "Herbivore", + "lifespan": "10-12 years", + "habitat": "Land", + "trait": "Provides wool", + "region": "Worldwide" + }, + { + "id": 8, + "name": "Pig", + "diet": "Omnivore", + "lifespan": "10-15 years", + "habitat": "Land", + "trait": "Intelligent", + "region": "Worldwide" + }, + { + "id": 9, + "name": "Duck", + "diet": "Omnivore", + "lifespan": "5-10 years", + "habitat": "Land and water", + "trait": "Webbed feet", + "region": "Worldwide" + }, + { + "id": 10, + "name": "Rabbit", + "diet": "Herbivore", + "lifespan": "8-12 years", + "habitat": "Land", + "trait": "Quick and small", + "region": "Worldwide" + }, + { + "id": 11, + "name": "Llama", + "diet": "Herbivore", + "lifespan": "15-25 years", + "habitat": "Land", + "trait": "Soft wool", + "region": "South America" + }, + { + "id": 12, + "name": "Donkey", + "diet": "Herbivore", + "lifespan": "25-30 years", + "habitat": "Land", + "trait": "Hard worker", + "region": "Worldwide" + }, + { + "id": 13, + "name": "Elephant", + "diet": "Herbivore", + "lifespan": "60-70 years", + "habitat": "Land", + "trait": "Largest land animal", + "region": "Africa, Asia" + }, + { + "id": 14, + "name": "Lion", + "diet": "Carnivore", + "lifespan": "10-14 years", + "habitat": "Land", + "trait": "King of the jungle", + "region": "Africa" + }, + { + "id": 15, + "name": "Tiger", + "diet": "Carnivore", + "lifespan": "10-15 years", + "habitat": "Land", + "trait": "Striped fur", + "region": "Asia" + }, + { + "id": 16, + "name": "Zebra", + "diet": "Herbivore", + "lifespan": "20-25 years", + "habitat": "Land", + "trait": "Striped body", + "region": "Africa" + }, + { + "id": 17, + "name": "Giraffe", + "diet": "Herbivore", + "lifespan": "25-30 years", + "habitat": "Land", + "trait": "Long neck", + "region": "Africa" + }, + { + "id": 18, + "name": "Kangaroo", + "diet": "Herbivore", + "lifespan": "20-23 years", + "habitat": "Land", + "trait": "Hops on two legs", + "region": "Australia" + }, + { + "id": 19, + "name": "Koala", + "diet": "Herbivore", + "lifespan": "13-18 years", + "habitat": "Land", + "trait": "Sleeps a lot", + "region": "Australia" + }, + { + "id": 20, + "name": "Panda", + "diet": "Herbivore", + "lifespan": "20-30 years", + "habitat": "Land", + "trait": "Loves bamboo", + "region": "China" + }, + { + "id": 21, + "name": "Chimpanzee", + "diet": "Omnivore", + "lifespan": "30-40 years", + "habitat": "Land", + "trait": "Tool user", + "region": "Africa" + }, + { + "id": 22, + "name": "Polar Bear", + "diet": "Carnivore", + "lifespan": "20-25 years", + "habitat": "Land and ice", + "trait": "Strong swimmer", + "region": "Arctic" + }, + { + "id": 23, + "name": "Grizzly Bear", + "diet": "Omnivore", + "lifespan": "20-25 years", + "habitat": "Land", + "trait": "Powerful predator", + "region": "North America" + }, + { + "id": 24, + "name": "Penguin", + "diet": "Carnivore", + "lifespan": "15-20 years", + "habitat": "Ocean and land", + "trait": "Cannot fly but swims", + "region": "Antarctica" + }, + { + "id": 25, + "name": "Dolphin", + "diet": "Carnivore", + "lifespan": "40-60 years", + "habitat": "Ocean", + "trait": "Highly intelligent", + "region": "Oceans worldwide" + }, + { + "id": 26, + "name": "Shark", + "diet": "Carnivore", + "lifespan": "20-30 years", + "habitat": "Ocean", + "trait": "Apex predator", + "region": "Oceans worldwide" + }, + { + "id": 27, + "name": "Whale", + "diet": "Carnivore", + "lifespan": "50-100 years", + "habitat": "Ocean", + "trait": "Largest animal", + "region": "Oceans worldwide" + }, + { + "id": 28, + "name": "Octopus", + "diet": "Carnivore", + "lifespan": "1-2 years", + "habitat": "Ocean", + "trait": "Eight arms", + "region": "Oceans worldwide" + }, + { + "id": 29, + "name": "Wolf", + "diet": "Carnivore", + "lifespan": "8-13 years", + "habitat": "Land", + "trait": "Pack hunter", + "region": "Worldwide (non-domesticated areas)" + }, + { + "id": 30, + "name": "Falcon", + "diet": "Carnivore", + "lifespan": "12-16 years", + "habitat": "Sky", + "trait": "Fastest bird", + "region": "Worldwide" + } + ] + \ No newline at end of file diff --git a/package.json b/package.json index f93ddb524..7dc72e6fe 100644 --- a/package.json +++ b/package.json @@ -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": [] } diff --git a/server.js b/server.js index b5fec6fe2..ebe1a26ea 100644 --- a/server.js +++ b/server.js @@ -1,27 +1,38 @@ 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(); // 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!"); +}); + +// 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 animal = animals.find((a) => a.id === id); + if (animal) { + res.json(animal); // Send the specific animal + } else { + res.status(404).json({ error: "Animal not found" }); + } }); // Start the server From 7dff80f71857fa55f3d8ebafa3148f9b59be1ccd Mon Sep 17 00:00:00 2001 From: Erika Olsson <erikamolsson87@gmail.com> Date: Fri, 6 Dec 2024 09:59:32 +0100 Subject: [PATCH 2/6] update: animals region - not working yet --- data/animals.json | 120 +++++++++++++++++++++++----------------------- server.js | 27 +++++++++-- 2 files changed, 84 insertions(+), 63 deletions(-) diff --git a/data/animals.json b/data/animals.json index b8fa8a2db..e555df680 100644 --- a/data/animals.json +++ b/data/animals.json @@ -1,273 +1,273 @@ [ { - "id": 1, "name": "Dog", "diet": "Omnivore", "lifespan": "10-13 years", "habitat": "Land", "trait": "Loyal", - "region": "Worldwide" + "region": "Worldwide", + "id": 1 }, { - "id": 2, "name": "Cat", "diet": "Carnivore", "lifespan": "12-16 years", "habitat": "Land", "trait": "Independent", - "region": "Worldwide" + "region": "Worldwide", + "id": 2 }, { - "id": 3, "name": "Horse", "diet": "Herbivore", "lifespan": "25-30 years", "habitat": "Land", "trait": "Strong and fast", - "region": "Worldwide" + "region": "Worldwide", + "id": 3 }, { - "id": 4, "name": "Cow", "diet": "Herbivore", "lifespan": "15-20 years", "habitat": "Land", "trait": "Produces milk", - "region": "Worldwide" + "region": "Worldwide", + "id": 4 }, { - "id": 5, "name": "Chicken", "diet": "Omnivore", "lifespan": "5-10 years", "habitat": "Land", "trait": "Lays eggs", - "region": "Worldwide" + "region": "Worldwide", + "id": 5 }, { - "id": 6, "name": "Goat", "diet": "Herbivore", "lifespan": "10-15 years", "habitat": "Land", "trait": "Climbs well", - "region": "Worldwide" + "region": "Worldwide", + "id": 6 }, { - "id": 7, "name": "Sheep", "diet": "Herbivore", "lifespan": "10-12 years", "habitat": "Land", "trait": "Provides wool", - "region": "Worldwide" + "region": "Worldwide", + "id": 7 }, { - "id": 8, "name": "Pig", "diet": "Omnivore", "lifespan": "10-15 years", "habitat": "Land", "trait": "Intelligent", - "region": "Worldwide" + "region": "Worldwide", + "id": 8 }, { - "id": 9, "name": "Duck", "diet": "Omnivore", "lifespan": "5-10 years", "habitat": "Land and water", "trait": "Webbed feet", - "region": "Worldwide" + "region": "Worldwide", + "id": 9 }, { - "id": 10, "name": "Rabbit", "diet": "Herbivore", "lifespan": "8-12 years", "habitat": "Land", "trait": "Quick and small", - "region": "Worldwide" + "region": "Worldwide", + "id": 10 }, { - "id": 11, "name": "Llama", "diet": "Herbivore", "lifespan": "15-25 years", "habitat": "Land", "trait": "Soft wool", - "region": "South America" + "region": "South America", + "id": 11 }, { - "id": 12, "name": "Donkey", "diet": "Herbivore", "lifespan": "25-30 years", "habitat": "Land", "trait": "Hard worker", - "region": "Worldwide" + "region": "Worldwide", + "id": 12 }, { - "id": 13, "name": "Elephant", "diet": "Herbivore", "lifespan": "60-70 years", "habitat": "Land", "trait": "Largest land animal", - "region": "Africa, Asia" + "region": "Africa, Asia", + "id": 13 }, { - "id": 14, "name": "Lion", "diet": "Carnivore", "lifespan": "10-14 years", "habitat": "Land", "trait": "King of the jungle", - "region": "Africa" + "region": "Africa", + "id": 14 }, { - "id": 15, "name": "Tiger", "diet": "Carnivore", "lifespan": "10-15 years", "habitat": "Land", "trait": "Striped fur", - "region": "Asia" + "region": "Asia", + "id": 15 }, { - "id": 16, "name": "Zebra", "diet": "Herbivore", "lifespan": "20-25 years", "habitat": "Land", "trait": "Striped body", - "region": "Africa" + "region": "Africa", + "id": 16 }, { - "id": 17, "name": "Giraffe", "diet": "Herbivore", "lifespan": "25-30 years", "habitat": "Land", "trait": "Long neck", - "region": "Africa" + "region": "Africa", + "id": 17 }, { - "id": 18, "name": "Kangaroo", "diet": "Herbivore", "lifespan": "20-23 years", "habitat": "Land", "trait": "Hops on two legs", - "region": "Australia" + "region": "Australia", + "id": 18 }, { - "id": 19, "name": "Koala", "diet": "Herbivore", "lifespan": "13-18 years", "habitat": "Land", "trait": "Sleeps a lot", - "region": "Australia" + "region": "Australia", + "id": 19 }, { - "id": 20, "name": "Panda", "diet": "Herbivore", "lifespan": "20-30 years", "habitat": "Land", "trait": "Loves bamboo", - "region": "China" + "region": "China", + "id": 20 }, { - "id": 21, "name": "Chimpanzee", "diet": "Omnivore", "lifespan": "30-40 years", "habitat": "Land", "trait": "Tool user", - "region": "Africa" + "region": "Africa", + "id": 21 }, { - "id": 22, "name": "Polar Bear", "diet": "Carnivore", "lifespan": "20-25 years", "habitat": "Land and ice", "trait": "Strong swimmer", - "region": "Arctic" + "region": "Arctic", + "id": 22 }, { - "id": 23, "name": "Grizzly Bear", "diet": "Omnivore", "lifespan": "20-25 years", "habitat": "Land", "trait": "Powerful predator", - "region": "North America" + "region": "North America", + "id": 23 }, { - "id": 24, "name": "Penguin", "diet": "Carnivore", "lifespan": "15-20 years", "habitat": "Ocean and land", "trait": "Cannot fly but swims", - "region": "Antarctica" + "region": "Antarctica", + "id": 24 }, { - "id": 25, "name": "Dolphin", "diet": "Carnivore", "lifespan": "40-60 years", "habitat": "Ocean", "trait": "Highly intelligent", - "region": "Oceans worldwide" + "region": "Oceans worldwide", + "id": 25 }, { - "id": 26, "name": "Shark", "diet": "Carnivore", "lifespan": "20-30 years", "habitat": "Ocean", "trait": "Apex predator", - "region": "Oceans worldwide" + "region": "Oceans worldwide", + "id": 26 }, { - "id": 27, "name": "Whale", "diet": "Carnivore", "lifespan": "50-100 years", "habitat": "Ocean", "trait": "Largest animal", - "region": "Oceans worldwide" + "region": "Oceans worldwide", + "id": 27 }, { - "id": 28, "name": "Octopus", "diet": "Carnivore", "lifespan": "1-2 years", "habitat": "Ocean", "trait": "Eight arms", - "region": "Oceans worldwide" + "region": "Oceans worldwide", + "id": 28 }, { - "id": 29, "name": "Wolf", "diet": "Carnivore", "lifespan": "8-13 years", "habitat": "Land", "trait": "Pack hunter", - "region": "Worldwide (non-domesticated areas)" + "region": "Worldwide (non-domesticated areas)", + "id": 29 }, { - "id": 30, "name": "Falcon", "diet": "Carnivore", "lifespan": "12-16 years", "habitat": "Sky", "trait": "Fastest bird", - "region": "Worldwide" + "region": "Worldwide", + "id": 30 } ] \ No newline at end of file diff --git a/server.js b/server.js index ebe1a26ea..87cea92bb 100644 --- a/server.js +++ b/server.js @@ -24,17 +24,38 @@ 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 animal = animals.find((a) => a.id === id); - if (animal) { - res.json(animal); // Send the specific animal + 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 region +app.get("/animals/region/:region", (req, res) => { + const region = req.params.region.toLowerCase(); + + console.log("Requested region:", region); + console.log("Available animals:", animals); + + const specificRegion = animals.filter((place) => + place.region.toLowerCase().includes(region) + ); + + if (specificRegion.lenght > 0 ) { + res.json(specificRegion); + } else { + res.status(404).json({ error: "Animal not found" }); + } +}); + + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); From 50e9d846a75fc5fbd1a59193b23e1b1eead14d88 Mon Sep 17 00:00:00 2001 From: Erika Olsson <erikamolsson87@gmail.com> Date: Fri, 6 Dec 2024 13:08:32 +0100 Subject: [PATCH 3/6] update: region working and find animal & its trait --- server.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/server.js b/server.js index 87cea92bb..f1838433a 100644 --- a/server.js +++ b/server.js @@ -37,26 +37,38 @@ app.get("/animals/:id", (req, res) => { }); -// Get animals from specific region -app.get("/animals/region/:region", (req, res) => { - const region = req.params.region.toLowerCase(); +// Get animals from specific regions +app.get("/regions/:region", (req, res) => { + const region = req.params.region; - console.log("Requested region:", region); - console.log("Available animals:", animals); - - const specificRegion = animals.filter((place) => - place.region.toLowerCase().includes(region) + const specificRegion = animals.filter((animal) => + animal.region.toLowerCase().includes(region) ); - - if (specificRegion.lenght > 0 ) { + + 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 + } + +}); + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); }); + From a10f445cc2ecdfeb378cbb35ec883327318aafc3 Mon Sep 17 00:00:00 2001 From: Erika Olsson <erikamolsson87@gmail.com> Date: Fri, 6 Dec 2024 14:39:36 +0100 Subject: [PATCH 4/6] readme file uodated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5241826b3..0467ebeef 100644 --- a/README.md +++ b/README.md @@ -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/ From 1aa56d36b53e75dcaf1cc519ca643fa7abd2db2d Mon Sep 17 00:00:00 2001 From: Erika Olsson <erikamolsson87@gmail.com> Date: Sat, 28 Dec 2024 18:37:13 +0100 Subject: [PATCH 5/6] update: endpoints in monepage --- package.json | 1 + server.js | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 7dc72e6fe..e6204e85a 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.21.1", + "express-list-endpoints": "^7.1.1", "nodemon": "^3.0.1" }, "main": "server.js", diff --git a/server.js b/server.js index f1838433a..d036658d7 100644 --- a/server.js +++ b/server.js @@ -9,6 +9,7 @@ import animals from "./data/animals.json" // PORT=9000 npm start 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()); @@ -16,7 +17,11 @@ app.use(express.json()); // Start "page" app.get("/", (req, res) => { - res.send("The animals of the world!"); + const endpoints = expressListEndpoints(app); + res.json({ + message: "The animals of the world!", + endpoints: endpoints + }); }); // Get alla animals from array From fa33f3829826a79693ced2527d4d6d2c9288b55a Mon Sep 17 00:00:00 2001 From: Erika Olsson <erikamolsson87@gmail.com> Date: Sat, 28 Dec 2024 18:43:17 +0100 Subject: [PATCH 6/6] missing semicolon.. --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index d036658d7..0b3335def 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,6 @@ import express from "express"; import cors from "cors"; -import animals from "./data/animals.json" +import animals from "./data/animals.json";