Skip to content

Commit

Permalink
Merge pull request #152 from Suyash878/master
Browse files Browse the repository at this point in the history
Improvements in the Bored API
  • Loading branch information
dishamodi0910 authored Jun 1, 2024
2 parents b8a8bb7 + 05f2aa7 commit 022b339
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Existing_API_Collection/Bored-API/Bored API/Activities.json
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 Existing_API_Collection/Bored-API/Bored API/server/index.js
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}`);
});

0 comments on commit 022b339

Please sign in to comment.