API calls added for live matches

This commit is contained in:
Lucas Patenaude
2024-04-03 22:05:41 -06:00
parent 31b84e44f7
commit 3a87b22d17
16 changed files with 249 additions and 102 deletions

View File

@@ -12,6 +12,7 @@ const bodyParser = require('body-parser');
const session = require('express-session'); // To set the session object. To store or access session data, use the `req.session`, which is (generally) serialized as JSON by the store.
const bcrypt = require('bcryptjs'); // To hash passwords
const axios = require('axios'); // To make HTTP requests from our server. We'll learn more about it in Part C.
const moment = require('moment'); // To extract current time data
// *****************************************************
// <!-- Section 2 : Connect to DB -->
@@ -150,16 +151,68 @@ app.get('/', (req, res) => {
res.redirect('/register');
}
});
/************************
Discover Page Routes
Scoreboard Header Routes
*************************/
// Render registration page for /register route
app.get('/home', (req, res) => {
res.render('pages/home');
// Connect to Premier League Matches API
// Define a middleware function to fetch Premier League matches data
app.get('/home', async (req, res) => {
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
}
});
// Log the response data
// console.log('API Response:', response.data);
const matches = response.data.matches.map(match => {
let homeScore, awayScore, minute;
// Log match data
console.log('Match Data:', {
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
});
});
// Render the Homepage
res.render('pages/home');
}
catch (error)
{
console.error('Error fetching Premier League matches:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// *****************************************************
// <!-- Section 5 : Start Server-->