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.
2024-03-15 03:21:45 -06:00
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
2024-03-12 01:56:44 -06:00
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
2024-04-10 21:53:51 -06:00
app . get ( '/welcome' , ( req , res ) => {
res . json ( { status : 'success' , message : 'Welcome!' } ) ;
} ) ;
2024-03-12 01:10:59 -06:00
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
2024-03-12 01:56:44 -06:00
app . use ( express . static ( path . join ( _ _dirname , 'resources' ) ) ) ;
2024-03-12 01:10:59 -06:00
// *****************************************************
// <!-- Section 4 : API Routes -->
// *****************************************************
2024-04-03 23:09:39 -06:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
Header Scoreboard Routes
* * * * * * * * * * * * * * * * * * * * * * * * * /
2024-04-05 00:00:07 -06:00
const fetchMatchesData = require ( './resources/js/navigation-bar/scoreboard-header/current-match-routes' ) ;
2024-04-03 23:09:39 -06:00
app . use ( fetchMatchesData ) ;
2024-04-05 00:00:07 -06:00
const convert _time = require ( './resources/js/navigation-bar/scoreboard-header/convert-time' ) ;
2024-04-04 22:16:16 -06:00
app . use ( convert _time ) ;
2024-04-09 21:10:08 -06:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
Homepage Routes
* * * * * * * * * * * * * * * * * * * * * * * * * /
2024-04-09 21:16:30 -06:00
/ * c o n s t { a p p , r e d i r e c t T o L e a g u e P a g e } = r e q u i r e ( ' P r o j e c t S o u r c e C o d e / s r c / r e s o u r c e s / j s / h o m e p a g e / c r e a t e - l e a g u e - r o u t e s . j s ' ) ;
2024-04-09 21:10:08 -06:00
// Serve static files
2024-04-09 21:16:30 -06:00
app . use ( express . static ( 'public' ) ) ; * /
2024-04-09 21:10:08 -06:00
2024-04-04 22:16:16 -06:00
2024-03-12 01:56:44 -06:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
Login Page Routes
* * * * * * * * * * * * * * * * * * * * * * * * * /
// Redirect to the /login endpoint
app . get ( '/' , ( req , res ) => {
2024-04-10 01:58:37 -06:00
res . redirect ( '/home' ) ;
2024-03-12 01:56:44 -06:00
} ) ;
// Render login page for /login route
app . get ( '/login' , ( req , res ) => {
2024-04-10 22:22:01 -06:00
res . render ( 'pages/home' ) ;
2024-03-12 01:56:44 -06:00
} ) ;
// 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' ) ;
} ) ;
2024-04-10 23:09:30 -06:00
// Trigger Registration Form to Post
app . post ( '/register' , async ( req , res ) => {
try {
if ( ! req . body . username || ! req . body . password ) {
// If username or password is missing, respond with status 400 and an error message
return res . status ( 400 ) . json ( { status : 'error' , message : 'Invalid input' } ) ;
2024-03-12 01:56:44 -06:00
}
2024-04-10 23:09:30 -06:00
// 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 ] ) ;
// Redirect user to the login page after successful registration
res . redirect ( '/login' ) ;
} catch ( error ) {
// If an error occurs during registration, respond with status 500 and an error message
res . status ( 500 ) . json ( { status : 'error' , message : 'An error occurred during registration' } ) ;
}
} ) ;
2024-04-10 21:53:51 -06:00
2024-03-12 01:56:44 -06:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
2024-04-03 23:09:39 -06:00
Home Page Routes
2024-03-12 01:56:44 -06:00
* * * * * * * * * * * * * * * * * * * * * * * * * /
2024-04-03 22:05:41 -06:00
2024-03-12 01:56:44 -06:00
app . get ( '/home' , ( req , res ) => {
res . render ( 'pages/home' ) ;
} ) ;
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
2024-04-10 21:53:51 -06:00
module . exports = app . listen ( 3000 ) ;
2024-03-12 01:10:59 -06:00
console . log ( 'Server is listening on port 3000' ) ;