ardasen commited on
Commit
2544e26
·
verified ·
1 Parent(s): 329471f

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +100 -0
server.js ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const axios = require('axios');
3
+ const NodeCache = require('node-cache');
4
+ const cors = require('cors');
5
+
6
+ const app = express();
7
+ const port = process.env.PORT || 3000;
8
+ const cache = new NodeCache({ stdTTL: 900 }); // Cache TTL is 15 minutes
9
+
10
+ const ERGAST_API_BASE_URL = 'https://ergast.com/api/f1/';
11
+
12
+ app.use(cors());
13
+
14
+ app.get('/api/currentSeason', async (req, res) => {
15
+ try {
16
+ let data = cache.get('currentSeason');
17
+ if (!data) {
18
+ const response = await axios.get(`${ERGAST_API_BASE_URL}current.json`);
19
+ data = response.data;
20
+ cache.set('currentSeason', data);
21
+ }
22
+ res.json(data);
23
+ } catch (error) {
24
+ const data = cache.get('currentSeason');
25
+ if (data) {
26
+ res.json(data);
27
+ } else {
28
+ res.status(500).send('Error fetching data from Ergast API and no cache available.');
29
+ }
30
+ }
31
+ });
32
+
33
+ app.get('/api/driverStandings', async (req, res) => {
34
+ const season = req.query.season || 'current';
35
+ try {
36
+ let data = cache.get(`driverStandings-${season}`);
37
+ if (!data) {
38
+ const response = await axios.get(`${ERGAST_API_BASE_URL}${season}/driverStandings.json`);
39
+ data = response.data;
40
+ cache.set(`driverStandings-${season}`, data);
41
+ }
42
+ res.json(data);
43
+ } catch (error) {
44
+ const data = cache.get(`driverStandings-${season}`);
45
+ if (data) {
46
+ res.json(data);
47
+ } else {
48
+ res.status(500).send('Error fetching data from Ergast API and no cache available.');
49
+ }
50
+ }
51
+ });
52
+
53
+ app.get('/api/constructorStandings', async (req, res) => {
54
+ const season = req.query.season || 'current';
55
+ try {
56
+ let data = cache.get(`constructorStandings-${season}`);
57
+ if (!data) {
58
+ const response = await axios.get(`${ERGAST_API_BASE_URL}${season}/constructorStandings.json`);
59
+ data = response.data;
60
+ cache.set(`constructorStandings-${season}`, data);
61
+ }
62
+ res.json(data);
63
+ } catch (error) {
64
+ const data = cache.get(`constructorStandings-${season}`);
65
+ if (data) {
66
+ res.json(data);
67
+ } else {
68
+ res.status(500).send('Error fetching data from Ergast API and no cache available.');
69
+ }
70
+ }
71
+ });
72
+
73
+ app.get('/api/raceResults', async (req, res) => {
74
+ const season = req.query.season || 'current';
75
+ const round = req.query.round;
76
+ if (!round) {
77
+ return res.status(400).send('Round is required.');
78
+ }
79
+
80
+ try {
81
+ let data = cache.get(`raceResults-${season}-${round}`);
82
+ if (!data) {
83
+ const response = await axios.get(`${ERGAST_API_BASE_URL}${season}/${round}/results.json`);
84
+ data = response.data;
85
+ cache.set(`raceResults-${season}-${round}`, data);
86
+ }
87
+ res.json(data);
88
+ } catch (error) {
89
+ const data = cache.get(`raceResults-${season}-${round}`);
90
+ if (data) {
91
+ res.json(data);
92
+ } else {
93
+ res.status(500).send('Error fetching data from Ergast API and no cache available.');
94
+ }
95
+ }
96
+ });
97
+
98
+ app.listen(port, () => {
99
+ console.log(`Server running on port ${port}`);
100
+ });