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 express api - deployed on Render - Emelie Nyberg #521
Open
EmelieNyberg
wants to merge
4
commits into
Technigo:master
Choose a base branch
from
EmelieNyberg: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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,31 @@ | ||
# Project Express API | ||
|
||
Replace this readme with your own information about your project. | ||
|
||
Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. | ||
This project is an introduction to backend development using Express. The main goal is to create a RESTful API with at least a few endpoints that return data from a hardcoded JSON dataset. These endpoints can return either collections or individual items based on client requests. | ||
|
||
## The problem | ||
|
||
Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? | ||
The challenge was to design and implement a functional API that adheres to RESTful principles. To achieve this, I planned the structure of the API by identifying potential endpoints and designing their responses based on user requirements. I used Node.js, Express, and array methods like `.find()`, `.filter()`, and `.sort()` to manipulate the JSON data effectively. | ||
|
||
I also added optional query parameters for filtering and sorting the dataset, making the API more dynamic and useful. If I had more time, I would consider adding: | ||
|
||
- Pagination for large datasets. | ||
- A frontend to visualize and interact with the API. | ||
- A connection to a database for persistent data storage. | ||
|
||
## 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. | ||
Render: https://project-express-api-ek.onrender.com | ||
|
||
## Features and Routes | ||
|
||
`GET /` | ||
Returns API documentation, listing all available endpoints. | ||
|
||
`GET /videogames` | ||
Returns all videogames or allows filtering by category (e.g., `?category=racing`) or release year (e.g., `?year=2020`). | ||
|
||
`GET /videogames/:id` | ||
Returns details of a single videogame based on its unique ID. | ||
|
||
`GET /videogames/sorted/ratings` | ||
Returns all videogames sorted by their rating, from highest to lowest. |
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,212 @@ | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "The Legend of Zelda: Breath of the Wild", | ||
"category": "Action-Adventure", | ||
"release_year": 2017, | ||
"rating": 9.5 | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "Super Mario Odyssey", | ||
"category": "Platformer", | ||
"release_year": 2017, | ||
"rating": 9.4 | ||
}, | ||
{ | ||
"id": 3, | ||
"name": "Mario Kart 8 Deluxe", | ||
"category": "Racing", | ||
"release_year": 2017, | ||
"rating": 9.3 | ||
}, | ||
{ | ||
"id": 4, | ||
"name": "Super Smash Bros. Ultimate", | ||
"category": "Fighting", | ||
"release_year": 2018, | ||
"rating": 9.2 | ||
}, | ||
{ | ||
"id": 5, | ||
"name": "Animal Crossing: New Horizons", | ||
"category": "Simulation", | ||
"release_year": 2020, | ||
"rating": 9.1 | ||
}, | ||
{ | ||
"id": 6, | ||
"name": "The Legend of Zelda: Tears of the Kingdom", | ||
"category": "Action-Adventure", | ||
"release_year": 2023, | ||
"rating": 9.0 | ||
}, | ||
{ | ||
"id": 7, | ||
"name": "Metroid Dread", | ||
"category": "Action", | ||
"release_year": 2021, | ||
"rating": 8.9 | ||
}, | ||
{ | ||
"id": 8, | ||
"name": "Splatoon 2", | ||
"category": "Shooter", | ||
"release_year": 2017, | ||
"rating": 8.8 | ||
}, | ||
{ | ||
"id": 9, | ||
"name": "Fire Emblem: Three Houses", | ||
"category": "Strategy", | ||
"release_year": 2019, | ||
"rating": 8.7 | ||
}, | ||
{ | ||
"id": 10, | ||
"name": "Xenoblade Chronicles 2", | ||
"category": "Role-Playing", | ||
"release_year": 2017, | ||
"rating": 8.6 | ||
}, | ||
{ | ||
"id": 11, | ||
"name": "Luigi's Mansion 3", | ||
"category": "Action-Adventure", | ||
"release_year": 2019, | ||
"rating": 8.5 | ||
}, | ||
{ | ||
"id": 12, | ||
"name": "Pokémon Sword and Shield", | ||
"category": "Role-Playing", | ||
"release_year": 2019, | ||
"rating": 8.4 | ||
}, | ||
{ | ||
"id": 13, | ||
"name": "Super Mario Party", | ||
"category": "Party", | ||
"release_year": 2018, | ||
"rating": 8.3 | ||
}, | ||
{ | ||
"id": 14, | ||
"name": "Octopath Traveler", | ||
"category": "Role-Playing", | ||
"release_year": 2018, | ||
"rating": 8.2 | ||
}, | ||
{ | ||
"id": 15, | ||
"name": "Hades", | ||
"category": "Action", | ||
"release_year": 2020, | ||
"rating": 8.1 | ||
}, | ||
{ | ||
"id": 16, | ||
"name": "The Witcher 3: Wild Hunt - Complete Edition", | ||
"category": "Role-Playing", | ||
"release_year": 2019, | ||
"rating": 8.0 | ||
}, | ||
{ | ||
"id": 17, | ||
"name": "Celeste", | ||
"category": "Platformer", | ||
"release_year": 2018, | ||
"rating": 7.9 | ||
}, | ||
{ | ||
"id": 18, | ||
"name": "Hollow Knight", | ||
"category": "Action-Adventure", | ||
"release_year": 2018, | ||
"rating": 7.8 | ||
}, | ||
{ | ||
"id": 19, | ||
"name": "Monster Hunter Rise", | ||
"category": "Action", | ||
"release_year": 2021, | ||
"rating": 7.7 | ||
}, | ||
{ | ||
"id": 20, | ||
"name": "Ring Fit Adventure", | ||
"category": "Fitness", | ||
"release_year": 2019, | ||
"rating": 7.6 | ||
}, | ||
{ | ||
"id": 21, | ||
"name": "Splatoon 3", | ||
"category": "Shooter", | ||
"release_year": 2022, | ||
"rating": 7.5 | ||
}, | ||
{ | ||
"id": 22, | ||
"name": "Mario + Rabbids Kingdom Battle", | ||
"category": "Strategy", | ||
"release_year": 2017, | ||
"rating": 7.4 | ||
}, | ||
{ | ||
"id": 23, | ||
"name": "Donkey Kong Country: Tropical Freeze", | ||
"category": "Platformer", | ||
"release_year": 2018, | ||
"rating": 7.3 | ||
}, | ||
{ | ||
"id": 24, | ||
"name": "Pokémon Legends: Arceus", | ||
"category": "Role-Playing", | ||
"release_year": 2022, | ||
"rating": 7.2 | ||
}, | ||
{ | ||
"id": 25, | ||
"name": "Kirby and the Forgotten Land", | ||
"category": "Platformer", | ||
"release_year": 2022, | ||
"rating": 7.1 | ||
}, | ||
{ | ||
"id": 26, | ||
"name": "Bayonetta 2", | ||
"category": "Action", | ||
"release_year": 2018, | ||
"rating": 7.0 | ||
}, | ||
{ | ||
"id": 27, | ||
"name": "Super Mario Maker 2", | ||
"category": "Platformer", | ||
"release_year": 2019, | ||
"rating": 6.9 | ||
}, | ||
{ | ||
"id": 28, | ||
"name": "Xenoblade Chronicles: Definitive Edition", | ||
"category": "Role-Playing", | ||
"release_year": 2020, | ||
"rating": 6.8 | ||
}, | ||
{ | ||
"id": 29, | ||
"name": "Astral Chain", | ||
"category": "Action", | ||
"release_year": 2019, | ||
"rating": 6.7 | ||
}, | ||
{ | ||
"id": 30, | ||
"name": "Pokémon Let's Go, Pikachu! and Let's Go, Eevee!", | ||
"category": "Role-Playing", | ||
"release_year": 2018, | ||
"rating": 6.6 | ||
} | ||
] |
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,18 +1,12 @@ | ||
import express from "express"; | ||
import cors from "cors"; | ||
|
||
// 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"; | ||
import listEndpoints from "express-list-endpoints"; | ||
import videogames from "./data/nintendoswitch-games.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 || 3000; | ||
const app = express(); | ||
|
||
// Add middlewares to enable cors and json body parsing | ||
|
@@ -21,7 +15,51 @@ app.use(express.json()); | |
|
||
// Start defining your routes here | ||
app.get("/", (req, res) => { | ||
res.send("Hello Technigo!"); | ||
res.json(listEndpoints(app)); | ||
}); | ||
|
||
// Endpoint that returns all the videogames | ||
app.get("/videogames", (req, res) => { | ||
const category = req.query.category; | ||
const year = req.query.year; | ||
|
||
// Filter on category if category is sent | ||
// For example http://localhost:3000/videogames?category=racing | ||
if (category) { | ||
const filterCategory = videogames.filter(game => game.category.toLowerCase() === category); | ||
res.json(filterCategory); | ||
|
||
// Endpoint that returns a specific year | ||
// For example http://localhost:3000/videogames?year=2020 | ||
} if (year) { | ||
const videogameByYear = videogames.filter(game => game.release_year === +year); | ||
res.json(videogameByYear); | ||
|
||
// If no categori is sent, return all videogames | ||
// For example http://localhost:3000/videogames | ||
} else { | ||
res.json(videogames); | ||
}; | ||
}); | ||
|
||
// Endpoint that returns one single videogame | ||
// For example with id=3 http://localhost:3000/videogames/3 | ||
app.get("/videogames/:id", (req, res) => { | ||
const id = req.params.id; | ||
const videogame = videogames.find(game => game.id === +id); | ||
|
||
if (videogame) { | ||
res.json(videogame); | ||
} else { | ||
res.send("No videogame found with that ID"); | ||
} | ||
}); | ||
|
||
// Endpoint that returns a list sorted by highest rating | ||
// For example http://localhost:3000/videogames/sorted/ratings | ||
app.get("/videogames/sorted/ratings", (req, res) => { | ||
const sortedGames = videogames.sort((a, b) => b.rating - a.rating); | ||
res.json(sortedGames); | ||
}); | ||
Comment on lines
+60
to
63
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. This could be another query param: |
||
|
||
// Start the server | ||
|
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.
Excellent filtering!