diff --git a/serve.js b/serve.js index 1c4a2ed..ad52d9f 100644 --- a/serve.js +++ b/serve.js @@ -2,11 +2,13 @@ const express = require('express'); const axios = require('axios'); const cheerio = require('cheerio'); const cron = require('node-cron'); +const cors = require('cors'); const app = express(); -let cachedRepoDetails = []; // Store the scraped data here +app.use(cors()); // Enable CORS + +let cachedRepoDetails = []; -// Function to scrape GitHub trending repositories const scrapeGitHubTrending = async () => { try { const { data } = await axios.get('https://github.com/trending'); @@ -28,30 +30,25 @@ const scrapeGitHubTrending = async () => { }); }); - cachedRepoDetails = repoDetails; // Update the cache with new data + cachedRepoDetails = repoDetails; } catch (error) { console.error('Error scraping GitHub trending data:', error); } }; -// Schedule the scraping to run every 7 hours cron.schedule('0 */7 * * *', () => { console.log('Fetching new data from GitHub Trending...'); scrapeGitHubTrending(); }); -// Initial fetch to have data ready when the server starts scrapeGitHubTrending(); -// Endpoint to serve the cached GitHub trending repositories feed app.get('/api/github-trending', (req, res) => { res.json(cachedRepoDetails); }); -// Serve static files (like your main website) app.use(express.static('public')); -// Start the server const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`);