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 ( 'bcrypt' ) ; // To hash passwords
const axios = require ( 'axios' ) ; // To make HTTP requests from our server. We'll learn more about it in Part C.
// *****************************************************
// <!-- 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 : 5433 , // the database port
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 , 'public' ) ) ) ;
// *****************************************************
// <!-- Section 4 : API Routes -->
// *****************************************************
// TODO - Include your API routes here
// *****************************************************
// <!-- 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' ) ;