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

Update requests.js #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
21 changes: 17 additions & 4 deletions services/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,23 @@ const getAllActivities = (req, res) => {
}

const getSingleActivity = (req, res) => {
fetch('https://www.boredapi.com/api/activity') // fetch activity from bored API - https://www.boredapi.com/about
.then(data => data.json()) // return a promise containing the response
.then(json => res.json(json)) // extract the JSON body content from the response (specifically the activity value) and sends it to the client
.catch((err) => console.log(err)) // log errors to the console
fetch('https://www.boredapi.com/api/activity')
.then(data => {
if (!data.ok) {
throw new Error(`HTTP error! status: ${data.status}`);
}
const contentType = data.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return data.json();
} else {
throw new Error("Received non-JSON response");
}
})
.then(json => res.json(json))
.catch((err) => {
console.error("Error fetching activity:", err);
res.status(500).json({ error: "Failed to fetch activity" });
});
}

const addActivityToDB = (req, res) => {
Expand Down