Skip to content

Commit

Permalink
Update serve.js
Browse files Browse the repository at this point in the history
  • Loading branch information
berlintay authored Aug 10, 2024
1 parent 6f4e9c3 commit 626f7a3
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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}`);
Expand Down

0 comments on commit 626f7a3

Please sign in to comment.