Update server.js
Browse files
server.js
CHANGED
|
@@ -1,69 +1,89 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
-
const
|
|
|
|
| 4 |
const cors = require('cors');
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
-
const port = process.env.PORT ||
|
| 8 |
-
|
| 9 |
-
const cache = apicache.middleware;
|
| 10 |
-
const cacheDuration = '15 minutes'; // Cache duration
|
| 11 |
|
| 12 |
const ERGAST_API_BASE_URL = 'https://ergast.com/api/f1/';
|
| 13 |
|
| 14 |
app.use(cors());
|
| 15 |
|
| 16 |
-
//
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
app.get('/api/currentSeason', async (req, res) => {
|
| 20 |
try {
|
| 21 |
-
const response = await axios.get(
|
| 22 |
-
|
|
|
|
| 23 |
} catch (error) {
|
| 24 |
-
console.error(
|
| 25 |
-
res.status(500).send('Error fetching data from Ergast API.');
|
| 26 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
});
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
try {
|
| 32 |
-
const
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
} catch (error) {
|
| 35 |
-
console.error(`Error fetching
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
});
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
app.get('/api/constructorStandings', async (req, res) => {
|
| 41 |
const season = req.query.season || 'current';
|
| 42 |
-
|
| 43 |
-
const response = await axios.get(`${ERGAST_API_BASE_URL}${season}/constructorStandings.json`);
|
| 44 |
-
res.json(response.data);
|
| 45 |
-
} catch (error) {
|
| 46 |
-
console.error(`Error fetching constructorStandings-${season} from Ergast API:`, error);
|
| 47 |
-
res.status(500).send('Error fetching data from Ergast API.');
|
| 48 |
-
}
|
| 49 |
});
|
| 50 |
|
|
|
|
| 51 |
app.get('/api/raceResults', async (req, res) => {
|
| 52 |
const season = req.query.season || 'current';
|
| 53 |
const round = req.query.round;
|
| 54 |
if (!round) {
|
| 55 |
return res.status(400).send('Round is required.');
|
| 56 |
}
|
| 57 |
-
|
| 58 |
-
try {
|
| 59 |
-
const response = await axios.get(`${ERGAST_API_BASE_URL}${season}/${round}/results.json`);
|
| 60 |
-
res.json(response.data);
|
| 61 |
-
} catch (error) {
|
| 62 |
-
console.error(`Error fetching raceResults-${season}-${round} from Ergast API:`, error);
|
| 63 |
-
res.status(500).send('Error fetching data from Ergast API.');
|
| 64 |
-
}
|
| 65 |
});
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
app.listen(port, () => {
|
| 68 |
console.log(`Server running on port ${port}`);
|
| 69 |
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
+
const NodeCache = require('node-cache');
|
| 4 |
+
const cron = require('node-cron');
|
| 5 |
const cors = require('cors');
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
+
const port = process.env.PORT || 3000;
|
| 9 |
+
const cache = new NodeCache({ stdTTL: 900 }); // Cache TTL is 15 minutes
|
|
|
|
|
|
|
| 10 |
|
| 11 |
const ERGAST_API_BASE_URL = 'https://ergast.com/api/f1/';
|
| 12 |
|
| 13 |
app.use(cors());
|
| 14 |
|
| 15 |
+
// Function to fetch data from Ergast API and update cache
|
| 16 |
+
const fetchDataAndUpdateCache = async (url, cacheKey) => {
|
|
|
|
|
|
|
| 17 |
try {
|
| 18 |
+
const response = await axios.get(url);
|
| 19 |
+
cache.set(cacheKey, response.data);
|
| 20 |
+
console.log(`Updated ${cacheKey} cache`);
|
| 21 |
} catch (error) {
|
| 22 |
+
console.error(`Error updating ${cacheKey} cache:`, error);
|
|
|
|
| 23 |
}
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
// Schedule to update cache every 15 minutes for all endpoints
|
| 27 |
+
cron.schedule('*/15 * * * *', async () => {
|
| 28 |
+
// Example: Update current season data
|
| 29 |
+
await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}current.json`, 'currentSeason');
|
| 30 |
+
|
| 31 |
+
// Add more endpoints to update cache as needed
|
| 32 |
+
// await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}current/other.json`, 'otherData');
|
| 33 |
});
|
| 34 |
|
| 35 |
+
// Generic endpoint handler to fetch data with caching and error handling
|
| 36 |
+
const handleEndpointWithCache = async (req, res, url, cacheKey) => {
|
| 37 |
try {
|
| 38 |
+
const cachedData = cache.get(cacheKey);
|
| 39 |
+
if (cachedData) {
|
| 40 |
+
res.json(cachedData);
|
| 41 |
+
} else {
|
| 42 |
+
const response = await axios.get(url);
|
| 43 |
+
cache.set(cacheKey, response.data);
|
| 44 |
+
res.json(response.data);
|
| 45 |
+
}
|
| 46 |
} catch (error) {
|
| 47 |
+
console.error(`Error fetching ${cacheKey} from Ergast API:`, error);
|
| 48 |
+
const cachedData = cache.get(cacheKey);
|
| 49 |
+
if (cachedData) {
|
| 50 |
+
res.json(cachedData);
|
| 51 |
+
} else {
|
| 52 |
+
res.status(500).send(`Error fetching data from Ergast API for ${cacheKey} and no cache available.`);
|
| 53 |
+
}
|
| 54 |
}
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
// Example endpoint to get current season data
|
| 58 |
+
app.get('/api/currentSeason', async (req, res) => {
|
| 59 |
+
await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}current.json`, 'currentSeason');
|
| 60 |
});
|
| 61 |
|
| 62 |
+
// Example endpoint to get driver standings data
|
| 63 |
+
app.get('/api/driverStandings', async (req, res) => {
|
| 64 |
+
const season = req.query.season || 'current';
|
| 65 |
+
await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}${season}/driverStandings.json`, `driverStandings-${season}`);
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
// Example endpoint to get constructor standings data
|
| 69 |
app.get('/api/constructorStandings', async (req, res) => {
|
| 70 |
const season = req.query.season || 'current';
|
| 71 |
+
await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}${season}/constructorStandings.json`, `constructorStandings-${season}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
});
|
| 73 |
|
| 74 |
+
// Example endpoint to get race results data
|
| 75 |
app.get('/api/raceResults', async (req, res) => {
|
| 76 |
const season = req.query.season || 'current';
|
| 77 |
const round = req.query.round;
|
| 78 |
if (!round) {
|
| 79 |
return res.status(400).send('Round is required.');
|
| 80 |
}
|
| 81 |
+
await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}${season}/${round}/results.json`, `raceResults-${season}-${round}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
});
|
| 83 |
|
| 84 |
+
// Add more endpoints with similar structure as needed
|
| 85 |
+
|
| 86 |
+
// Start server
|
| 87 |
app.listen(port, () => {
|
| 88 |
console.log(`Server running on port ${port}`);
|
| 89 |
});
|