Files
ScoreSpot/ProjectSourceCode/src/index.js

222 lines
6.9 KiB
JavaScript
Raw Normal View History

2024-03-12 01:10:59 -06:00
// *****************************************************
// <!-- Section 1 : Import Dependencies -->
// *****************************************************
const express = require('express'); // To build an application server or API
const app = express();
const handlebars = require('express-handlebars');
const Handlebars = require('handlebars');
const path = require('path');
const pgp = require('pg-promise')(); // To connect to the Postgres DB from the node server
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
2024-03-12 01:10:59 -06:00
const axios = require('axios'); // To make HTTP requests from our server. We'll learn more about it in Part C.
2024-04-03 22:05:41 -06:00
const moment = require('moment'); // To extract current time data
2024-03-12 01:10:59 -06:00
// *****************************************************
// <!-- Section 2 : Connect to DB -->
// *****************************************************
// create `ExpressHandlebars` instance and configure the layouts and partials dir.
const hbs = handlebars.create({
extname: 'hbs',
layoutsDir: __dirname + '/views/layouts',
partialsDir: __dirname + '/views/partials',
});
// database configuration
const dbConfig = {
host: 'db', // the database server
port: 5432, // the database port
2024-03-12 01:10:59 -06:00
database: process.env.POSTGRES_DB, // the database name
user: process.env.POSTGRES_USER, // the user account to connect with
password: process.env.POSTGRES_PASSWORD, // the password of the user account
};
const db = pgp(dbConfig);
// test your database
db.connect()
.then(obj => {
console.log('Database connection successful'); // you can view this message in the docker compose logs
obj.done(); // success, release the connection;
})
.catch(error => {
console.log('ERROR:', error.message || error);
});
// *****************************************************
// <!-- Section 3 : App Settings -->
// *****************************************************
// Register `hbs` as our view engine using its bound `engine()` function.
app.engine('hbs', hbs.engine);
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.json()); // specify the usage of JSON for parsing request body.
// initialize session variables
app.use(
session({
secret: process.env.SESSION_SECRET,
saveUninitialized: false,
resave: false,
})
);
app.use(
bodyParser.urlencoded({
extended: true,
})
);
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'resources')));
2024-03-12 01:10:59 -06:00
// *****************************************************
// <!-- Section 4 : API Routes -->
// *****************************************************
/************************
Login Page Routes
*************************/
// Redirect to the /login endpoint
app.get('/', (req, res) => {
res.redirect('/login');
});
// Render login page for /login route
app.get('/login', (req, res) => {
res.render('pages/login');
});
// Trigger login form to check database for matching username and password
app.post('/login', async (req, res) => {
try {
// Check if username exists in DB
const user = await db.oneOrNone('SELECT * FROM users WHERE username = $1', req.body.username);
if (!user) {
// Redirect user to login screen if no user is found with the provided username
return res.redirect('/register');
}
// Check if password from request matches with password in DB
const match = await bcrypt.compare(req.body.password, user.password);
// Check if mathc returns no data
if (!match) {
// Render the login page with the message parameter
return res.render('pages/login', { message: 'Password does not match' });
}
// Save user information in the session variable
req.session.user = user;
req.session.save();
// Redirect user to the home page
res.redirect('/home');
} catch (error) {
// Direct user to login screen if no user is found with matching password
res.redirect('/register');
}
});
/************************
Registration Page Routes
*************************/
// Render registration page for /register route
app.get('/register', (req, res) => {
res.render('pages/register');
});
// Trigger Registration Form to Post
app.post('/register', async (req, res) => {
try {
// Hash the password using bcrypt library
const hash = await bcrypt.hash(req.body.password, 10);
// Insert username and hashed password into the 'users' table
await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]);
// Direct user to login screen after data has been inserted successfully
res.redirect('/login');
} catch (error) {
// If the insert fails, redirect to GET /register route
res.redirect('/register');
}
});
2024-04-03 22:05:41 -06:00
/************************
2024-04-03 22:05:41 -06:00
Scoreboard Header Routes
*************************/
2024-04-03 22:05:41 -06:00
// 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
});
});
2024-03-12 01:10:59 -06:00
2024-04-03 22:05:41 -06:00
// 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' });
}
});
2024-03-12 01:10:59 -06:00
// *****************************************************
// <!-- Section 5 : Start Server-->
// *****************************************************
// starting the server and keeping the connection open to listen for more requests
app.listen(3000);
console.log('Server is listening on port 3000');