Added Moment module and got data imported into each card

This commit is contained in:
Lucas Patenaude
2024-04-03 23:09:39 -06:00
parent 3a87b22d17
commit d7e7372776
1088 changed files with 186474 additions and 116 deletions

View File

@@ -0,0 +1,48 @@
const moment = require('moment');
const axios = require('axios');
// Middleware function to fetch matches data
const fetchMatchesData = async (req, res, next) => {
try {
const today = moment().format('YYYY-MM-DD'); // Get today's date in YYYY-MM-DD format
const response = await axios({
url: 'http://api.football-data.org/v4/competitions/2021/matches',
method: 'GET',
params: {
dateFrom: '2024-04-03', // Set dateFrom to today's date
dateTo: today, // Set dateTo to today's date
},
headers: {
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
},
});
// Extract relevant data from the API response
const matches = response.data.matches.map(match => ({
homeTeam: {
name: match.homeTeam.name,
crest: match.homeTeam.crest,
},
awayTeam: {
name: match.awayTeam.name,
crest: match.awayTeam.crest,
},
score: {
homeScore: match.score.fullTime.home,
awayScore: match.score.fullTime.away,
},
minute: match.status, // Set the minute of the game
}));
// Attach matches data to res.locals
res.locals.matches = matches;
next();
} catch (error) {
console.error('Error fetching Premier League matches:', error);
res.locals.matches = []; // Set an empty array if there's an error
next(); // Call next middleware or route handler
}
};
module.exports = fetchMatchesData;