Update server.js
Browse files
server.js
CHANGED
|
@@ -1,160 +1,36 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
-
const
|
| 4 |
-
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const port = process.env.PORT || 3000;
|
| 8 |
-
const
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return new Promise((resolve, reject) => {
|
| 23 |
-
client.get(key, (err, data) => {
|
| 24 |
-
if (err) {
|
| 25 |
-
return reject(err);
|
| 26 |
-
}
|
| 27 |
-
if (data) {
|
| 28 |
-
return resolve(JSON.parse(data));
|
| 29 |
-
}
|
| 30 |
-
return resolve(null);
|
| 31 |
-
});
|
| 32 |
-
});
|
| 33 |
-
};
|
| 34 |
-
|
| 35 |
-
const setCachedData = (key, data) => {
|
| 36 |
-
return new Promise((resolve, reject) => {
|
| 37 |
-
client.setex(key, cacheTTL, JSON.stringify(data), (err) => {
|
| 38 |
-
if (err) {
|
| 39 |
-
return reject(err);
|
| 40 |
-
}
|
| 41 |
-
resolve();
|
| 42 |
-
});
|
| 43 |
-
});
|
| 44 |
-
};
|
| 45 |
-
|
| 46 |
-
app.get('/api/currentSeason', async (req, res) => {
|
| 47 |
-
try {
|
| 48 |
-
const cacheKey = 'currentSeason';
|
| 49 |
-
let data = await getCachedData(cacheKey);
|
| 50 |
-
if (!data) {
|
| 51 |
-
console.log('Cache miss for currentSeason');
|
| 52 |
-
const response = await axios.get(`${ERGAST_API_BASE_URL}current.json`);
|
| 53 |
-
data = response.data;
|
| 54 |
-
await setCachedData(cacheKey, data);
|
| 55 |
-
console.log('Data fetched from Ergast API and cached for currentSeason');
|
| 56 |
-
} else {
|
| 57 |
-
console.log('Cache hit for currentSeason');
|
| 58 |
-
}
|
| 59 |
-
res.json(data);
|
| 60 |
-
} catch (error) {
|
| 61 |
-
console.error('Error fetching currentSeason from Ergast API:', error);
|
| 62 |
-
const data = await getCachedData('currentSeason');
|
| 63 |
-
if (data) {
|
| 64 |
-
console.log('Returning cached data for currentSeason due to error');
|
| 65 |
-
res.json(data);
|
| 66 |
-
} else {
|
| 67 |
-
res.status(500).send('Error fetching data from Ergast API and no cache available.');
|
| 68 |
-
}
|
| 69 |
}
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
const
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
console.log(`Cache miss for driverStandings-${season}`);
|
| 79 |
-
const response = await axios.get(`${ERGAST_API_BASE_URL}${season}/driverStandings.json`);
|
| 80 |
-
data = response.data;
|
| 81 |
-
await setCachedData(cacheKey, data);
|
| 82 |
-
console.log(`Data fetched from Ergast API and cached for driverStandings-${season}`);
|
| 83 |
-
} else {
|
| 84 |
-
console.log(`Cache hit for driverStandings-${season}`);
|
| 85 |
-
}
|
| 86 |
-
res.json(data);
|
| 87 |
-
} catch (error) {
|
| 88 |
-
console.error(`Error fetching driverStandings-${season} from Ergast API:`, error);
|
| 89 |
-
const data = await getCachedData(`driverStandings-${season}`);
|
| 90 |
-
if (data) {
|
| 91 |
-
console.log(`Returning cached data for driverStandings-${season} due to error`);
|
| 92 |
-
res.json(data);
|
| 93 |
-
} else {
|
| 94 |
-
res.status(500).send('Error fetching data from Ergast API and no cache available.');
|
| 95 |
-
}
|
| 96 |
-
}
|
| 97 |
-
});
|
| 98 |
-
|
| 99 |
-
app.get('/api/constructorStandings', async (req, res) => {
|
| 100 |
-
const season = req.query.season || 'current';
|
| 101 |
-
try {
|
| 102 |
-
const cacheKey = `constructorStandings-${season}`;
|
| 103 |
-
let data = await getCachedData(cacheKey);
|
| 104 |
-
if (!data) {
|
| 105 |
-
console.log(`Cache miss for constructorStandings-${season}`);
|
| 106 |
-
const response = await axios.get(`${ERGAST_API_BASE_URL}${season}/constructorStandings.json`);
|
| 107 |
-
data = response.data;
|
| 108 |
-
await setCachedData(cacheKey, data);
|
| 109 |
-
console.log(`Data fetched from Ergast API and cached for constructorStandings-${season}`);
|
| 110 |
-
} else {
|
| 111 |
-
console.log(`Cache hit for constructorStandings-${season}`);
|
| 112 |
-
}
|
| 113 |
-
res.json(data);
|
| 114 |
-
} catch (error) {
|
| 115 |
-
console.error(`Error fetching constructorStandings-${season} from Ergast API:`, error);
|
| 116 |
-
const data = await getCachedData(`constructorStandings-${season}`);
|
| 117 |
-
if (data) {
|
| 118 |
-
console.log(`Returning cached data for constructorStandings-${season} due to error`);
|
| 119 |
-
res.json(data);
|
| 120 |
-
} else {
|
| 121 |
-
res.status(500).send('Error fetching data from Ergast API and no cache available.');
|
| 122 |
-
}
|
| 123 |
-
}
|
| 124 |
-
});
|
| 125 |
-
|
| 126 |
-
app.get('/api/raceResults', async (req, res) => {
|
| 127 |
-
const season = req.query.season || 'current';
|
| 128 |
-
const round = req.query.round;
|
| 129 |
-
if (!round) {
|
| 130 |
-
return res.status(400).send('Round is required.');
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
try {
|
| 134 |
-
const cacheKey = `raceResults-${season}-${round}`;
|
| 135 |
-
let data = await getCachedData(cacheKey);
|
| 136 |
-
if (!data) {
|
| 137 |
-
console.log(`Cache miss for raceResults-${season}-${round}`);
|
| 138 |
-
const response = await axios.get(`${ERGAST_API_BASE_URL}${season}/${round}/results.json`);
|
| 139 |
-
data = response.data;
|
| 140 |
-
await setCachedData(cacheKey, data);
|
| 141 |
-
console.log(`Data fetched from Ergast API and cached for raceResults-${season}-${round}`);
|
| 142 |
-
} else {
|
| 143 |
-
console.log(`Cache hit for raceResults-${season}-${round}`);
|
| 144 |
-
}
|
| 145 |
-
res.json(data);
|
| 146 |
-
} catch (error) {
|
| 147 |
-
console.error(`Error fetching raceResults-${season}-${round} from Ergast API:`, error);
|
| 148 |
-
const data = await getCachedData(`raceResults-${season}-${round}`);
|
| 149 |
-
if (data) {
|
| 150 |
-
console.log(`Returning cached data for raceResults-${season}-${round} due to error`);
|
| 151 |
-
res.json(data);
|
| 152 |
-
} else {
|
| 153 |
-
res.status(500).send('Error fetching data from Ergast API and no cache available.');
|
| 154 |
-
}
|
| 155 |
}
|
|
|
|
| 156 |
});
|
| 157 |
|
| 158 |
app.listen(port, () => {
|
| 159 |
-
|
| 160 |
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
+
const NodeCache = require('node-cache');
|
|
|
|
| 4 |
|
| 5 |
const app = express();
|
| 6 |
const port = process.env.PORT || 3000;
|
| 7 |
+
const cache = new NodeCache({ stdTTL: 900 }); // Cache TTL is 15 minutes
|
| 8 |
+
|
| 9 |
+
const ERGAST_API_URL = 'https://ergast.com/api/f1/current.json';
|
| 10 |
+
|
| 11 |
+
app.get('/api/f1', async (req, res) => {
|
| 12 |
+
try {
|
| 13 |
+
// Check if data is in cache
|
| 14 |
+
let data = cache.get('f1Data');
|
| 15 |
+
if (!data) {
|
| 16 |
+
// If not, fetch from Ergast API
|
| 17 |
+
const response = await axios.get(ERGAST_API_URL);
|
| 18 |
+
data = response.data;
|
| 19 |
+
// Store data in cache
|
| 20 |
+
cache.set('f1Data', data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
+
res.json(data);
|
| 23 |
+
} catch (error) {
|
| 24 |
+
// On error, check cache
|
| 25 |
+
const data = cache.get('f1Data');
|
| 26 |
+
if (data) {
|
| 27 |
+
res.json(data);
|
| 28 |
+
} else {
|
| 29 |
+
res.status(500).send('Error fetching data from Ergast API and no cache available.');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
+
}
|
| 32 |
});
|
| 33 |
|
| 34 |
app.listen(port, () => {
|
| 35 |
+
console.log(`Server running on port ${port}`);
|
| 36 |
});
|