This is our CA1 Project on UNO-Clone, created as a PERN project and hosted in heroku. P - Postgres E - Express R - React N - Nodejs
Visit out website here : https://uno-clone.herokuapp.com/
sql CREATE DATABASE uno_clone;
sql CREATE TABLE uno_cards;
bash
psql -U postgres
sql \l => list all databases in postgrea
\c => enter the specific database
\dt => show tables in database
Starting and setting up our server bash mkdir server cd server npm init
Creating the server with express framework bash touch index.js npm install express pg cors
js const devConfig = { user: process.env.PG_USER, password: process.env.PG_PASSWORD, host: process.env.PG_HOST, port: process.env.PG_PORT, database: process.env.PG_DATABASE }
This will tell the server to determine which configuration to use in devlopment and in production.
bash npx create-react-app client
bash npm start
Heroku requires the package.json file in root directory as it will be instructions for heroku. Therefore, our server files will be moved to the top level directory.
In our reactJS frontend, we have to create a build folder for the server to tell heroku to run bash npm run build
To tell our server to run static content in production, we will do bash if (process.env.NODE_ENV === "production") { app.use(express.static(path.join(__dirname, "client/build"))); }
This tells the express server to use the static directory which points to the build folder in client when in production
We will also hide our environment variables with process.env This is done with .env and .gitignore file bash npm i install dotenv
To create Pool connection based on local database and heroku database, we will seperate the configuration by using bash const Pool = require("pg").Pool; require("dotenv").config();
const devConfig = { user: process.env.PG_USER, password: process.env.PG_PASSWORD, host: process.env.PG_HOST, port: process.env.PG_PORT, database: process.env.PG_DATABASE }
const proConfig = { connectionString: process.env.DATABASE_URL // heroku addons }
const pool = new Pool(process.env.NODE_ENV === "production" ? proConfig : devConfig);
In package.json, specify codes for heroku to run while building project for deployment bash "scripts": { "start": "node index.js", "heroku-postbuild": "cd client && npm install && npm run build" },
Order of heroku deployment
- run the prebuild script before dependencies are installed
- npm install will run to install dependencies
- heroku-postbuild script is then run after dependencies are installed
- start script is run for server to start
Proxy will help production by using heroku domain when accessing backend api instead of localhost In package.json, specify codes for heroku to run while building project for deplyment bash "proxy": "localhost:5000"
As the heroku app will serve our static build content along with restful api, we can specify endpoints through paths. However, in development, we have to specify the backend URL so the proxy will do its job here for easy editing in development
bash "engines": { "node": "15.0.0", "npm": "7.0.2" },
bash
heroku login
heroku create uno-clone

Add addons for postgres database service for heroku application
bash
heroku addons:create heroku-postgresql:hoddy-dev -a uno-clone

bash
heroku pg:psql -a uno-clone
heroku git:remote -a uno-clone
heroku open

