Step-by-step guide to creating a basic blog app in node and express backed by postgres
You have been hired to complete an API for a blog app. The previous developer won the lottery or got hit by a bus, no one really knows. It's kinda a mystery and it is all anyone talks about around the office. Unfortunately Sherlock, that's not the puzzle you were hired to solve. Your challenge is to complete the blog app in time for the CEO's demo to the board next week.
- The previous developer created a front-end GUI client using jQuery and Fetch. And started a API using Node and Express.
- The API currently uses dummy data which is just an array of objects in Javascript. and is not wired up to a database yet.
- The company uses PostgreSQL as their database. And hosts their applications on Heroku.
- Your manager says the specs for the API were documented using Postman's documenter and can be found here: Blog App API.
- You are scheduled to have a meeting this morning at 10:00 AM Eastern to review the front-end client.
- And you have a follow-up meeting scheduled for 1:30 PM Eastern to answer any questions.
- Lastly, you found a todo list and some notes which list the tasks still needed to be completed.
-
Create a Skeleton Node and Express app
-
Create set of CRUD endpoints at
/api/v1/stories
- Create a GET endpoint to list all stories - should return status 200 and an array of objects
- Create a GET endpoint to get a story by id - should return status 200 and an object
- Create a POST endpoint to create a story - should return status 201 with a location header and the new object
- Create a PUT endpoint to update a story - should return status 200 and the updated object
- Create a DELETE endpoint to remove a story - should return status 204 with no content
-
Create and wire-up database to endpoints
- Create local Postgres database named
blog-app
(See:createdb
on SQL shell basics )
Note-to-self: If your local database doesn't work, try using ElephantSQL. But remember to put the connection string in an environment variable and use
dotenv
to prevent the UN/PW from being pushed to GitHub. - Create local Postgres database named
-
Create a
stories
table with the following schema.- 3 Columns:
id
: an autoincrementing integertitle
: regular text. Required.content
: regular ol' text. Can be blank.
Note-to-self: remember to save the CREATE TABLE and INSERT INTO queries in a file so they can be easily run again later. Examples:
psql -f ./query.sql -U <username> -d blog-app postgres://<USERNAME>:<PASSWORD>@<SERVER:PORT>/<DATABASE>
- 3 Columns:
-
Update
config.js
with DB connection info (see node-restaurants-app-knex) -
Replace dummy data with real database calls
- Update GET
/api/v1/stories
endpoint to useknex.select()...
- Update GET
/api/v1/stories/:id
endpoint to useknex.select()...
- Update POST
/api/v1/stories/
endpoint to useknex.insert()...
- Update PUT
/api/v1/stories/:id
endpoint to useknex.update()...
- Update DELETE
/api/v1/stories/:id
endpoint to useknex.del()...
- Update GET
-
Deploy to Heroku (see Deploying to Heroku)
- Install Heroku CLI app, if necessary
- Create app on Heroku
- Update git remote to point to Heroku
- Push app to Heroku - Note: app probably won't work yet 'cause there's no production database
- Create a production database on Elephant SQL. Use
query.sql
from earlier to easily recreate database - Configure Heroku config vars to use
DATABASE_URL
and the Elephant SQL connection string