-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from Suyash878/master
Improvements in the Bored API
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
Existing_API_Collection/Bored-API/Bored API/Activities.json
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,83 @@ | ||
[ | ||
{ | ||
"id": 1, | ||
"activity": "Read a book" | ||
}, | ||
{ | ||
"id": 2, | ||
"activity": "Go for a walk in the park" | ||
}, | ||
{ | ||
"id": 3, | ||
"activity": "Try a new recipe" | ||
}, | ||
{ | ||
"id": 4, | ||
"activity": "Learn a new language" | ||
}, | ||
{ | ||
"id": 5, | ||
"activity": "Practice meditation or yoga" | ||
}, | ||
{ | ||
"id": 6, | ||
"activity": "Write in a journal" | ||
}, | ||
{ | ||
"id": 7, | ||
"activity": "Watch a documentary" | ||
}, | ||
{ | ||
"id": 8, | ||
"activity": "Take up gardening" | ||
}, | ||
{ | ||
"id": 9, | ||
"activity": "Try a new hobby like painting or knitting" | ||
}, | ||
{ | ||
"id": 10, | ||
"activity": "Visit a museum or art gallery" | ||
}, | ||
{ | ||
"id": 11, | ||
"activity": "Volunteer for a local charity" | ||
}, | ||
{ | ||
"id": 12, | ||
"activity": "Listen to a podcast" | ||
}, | ||
{ | ||
"id": 13, | ||
"activity": "Have a picnic in the backyard" | ||
}, | ||
{ | ||
"id": 14, | ||
"activity": "Explore a nearby town or city" | ||
}, | ||
{ | ||
"id": 15, | ||
"activity": "Take a photography walk" | ||
}, | ||
{ | ||
"id": 16, | ||
"activity": "Have a board game night with friends or family" | ||
}, | ||
{ | ||
"id": 17, | ||
"activity": "Try a DIY project" | ||
}, | ||
{ | ||
"id": 18, | ||
"activity": "Attend a virtual workshop or webinar" | ||
}, | ||
{ | ||
"id": 19, | ||
"activity": "Organize your living space" | ||
}, | ||
{ | ||
"id": 20, | ||
"activity": "Plan a future trip or adventure" | ||
} | ||
] | ||
|
34 changes: 34 additions & 0 deletions
34
Existing_API_Collection/Bored-API/Bored API/server/index.js
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,34 @@ | ||
const express = require('express'); | ||
const axios = require('axios'); // Import Axios | ||
|
||
const app = express(); | ||
const PORT = process.env.PORT || 3000; | ||
|
||
// Serve static files (HTML, CSS, JavaScript) | ||
app.use(express.static('public')); | ||
|
||
// Define route to handle the client's request for a random activity | ||
app.get('/randomActivity', async (req, res) => { | ||
try { | ||
const apiUrl = 'https://suyash878.github.io/JSON_boredAPI_Activities/Activities.json'; | ||
const response = await axios.get(apiUrl); // Use Axios to make GET request | ||
|
||
if (!response.data) { | ||
throw new Error('Empty response from the API'); | ||
} | ||
|
||
// Pick a random activity from the response data array | ||
const randomIndex = Math.floor(Math.random() * response.data.length); | ||
const randomActivity = response.data[randomIndex].activity; | ||
|
||
res.json({activity: randomActivity}); | ||
} catch (error) { | ||
console.error('Error fetching activity:', error); | ||
res.status(500).json({ error: 'Failed to fetch activity' }); | ||
} | ||
}); | ||
|
||
// Start the server | ||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |