You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PostgreSQL Express React Node (PERN) full-stack app, integrates React frontend with Node.js backend that is deployed to Heroku. Tutorial code by The Stoic Programmers (see 'Inspiration' below)
Note: to open web links in a new window use: ctrl+click on link
Install nodemon globally if you don't already have it
Install PostgreSQL & run it (requires the password you created during installation)
Add database access credentials to db.js - recommend installing npm dotenv & using .env to hide credentials if commiting to Github
Postgresql shell commands: \l list all databases. \c database1 connect to database1. \dt inspect tables. \d+ inspect table & show relation information. \q to quit
From root run nodemon server for a dev server
http://localhost:5000/ can be accessed for CRUD operations such as POST, GET, PUT, DELETE etc. using Postman
💾 Dev Setup - Frontend
Change to client directory
Install dependencies using npm i.
run npm start. Frontend will open at http://localhost:3000/
client files edited: change to use proxy for dev address
Once package.json scripts added and you are logged into Heroku:
Create project: heroku create pern-stack-todoapp
Add Heroku database addon: heroku addons:create heroku-postgresql:hobby-dev -a pern-stack-todoapp
To access Heroku database: pg:psql -a pern-stack-todoapp
Run git add . then git commit -m "code added for Heroku deployment" to add all changes
Run heroku git:remote -a pern-stack-todoapp - this is my example
Run git push heroku master to send app to Heroku
Run heroku open to open app in a browser window
💻 Code Examples - Backend
backend index.js: express post method used to add new todo [description] to postgreSQL database using SQL INSERT INTO statement
// create a todoapp.post('/todos',async(req,res)=>{try{const{ description }=req.body;constnewTodo=awaitpool.query("INSERT INTO todo (description) VALUES($1) RETURNING *",[description]);res.json(newTodo.rows[0]);}catch(err){console.error(err.message);}})
💻 Code Examples - Frontend
function that runs when user presses 'Add' button: the input body (description) is converted from a JavaScript object to a JSON string & POSTed to the todo database