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/ diff --git a/data/animals.json b/data/animals.json new file mode 100644 index 000000000..e555df680 --- /dev/null +++ b/data/animals.json @@ -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 + } + ] + \ No newline at end of file diff --git a/package.json b/package.json index f93ddb524..e6204e85a 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,10 @@ "@babel/node": "^7.16.8", "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", - "express": "^4.17.3", + "express": "^4.21.1", + "express-list-endpoints": "^7.1.1", "nodemon": "^3.0.1" - } + }, + "main": "server.js", + "keywords": [] } diff --git a/server.js b/server.js index b5fec6fe2..0b3335def 100644 --- a/server.js +++ b/server.js @@ -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 + } + }); // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); }); +