In this repository, I demonstrate how to build a very TODO API with simple CRUD functionality using node.js
and postgresql
and spin it up using docker-compose
- Create a folder for this application
mkdir todo-app
cd todo-app
- Initialize git repository and
.gitignore
git init
curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
- Initialize a node.js express app with postgres client
npm init -y
npm i express pg -s
- Update description/author/license in
package.json
- Coding persistence layer in
queries.js
- Coding app and route layer in
index.js
- Add
Dockerfile
&.dockerignore
- Use Docker Compose for dev environment, create a file
docker-compose.yml
with 2 services
web
web:
build: .
ports:
- "3000:3000"
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: mysecretpassword
POSTGRES_DB: todo
POSTGRES_HOST: pg
POSTGRES_PORT: 5432
links:
- pg
postgres
pg:
image: postgres:12
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data
- ./init-todo-db.sql:/docker-entrypoint-initdb.d/init-todo-db.sql
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: mysecretpassword
POSTGRES_DB: todo
- Add db initialization script
init-todo-db.sql
- Add
README.md
andLICENSE.md
docker-compose build
docker-compose up
curl -d "task=awesome task 1" -X POST localhost:3000/todos
curl -d "task=awesome task 2" -X POST localhost:3000/todos
curl localhost:3000/todos
curl -d "complete=true" -X PUT localhost:3000/todos/[id]
Everything looks fine, time to commit code locally
git add .
git commit -m "Initial commit"
Go ahead to create a Github repository and push commits to remote
gh repo create
Example outputs
➜ todo-app git:(main) gh repo create
? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name todo-app
? Description Simple Todo CRUD API using node.js and PostgreSQL
? Visibility Private
✓ Created repository davidsung/todo-app on GitHub
? Add a remote? Yes
? What should the new remote be called? origin
✓ Added remote https://github.com/somegeek/awesome-repo.git
? Would you like to push commits from the current branch to the "origin"? Yes
✓ Pushed commits to https://github.com/somegeek/awesome-repo.git
Open up a browser to check
gh repo view --web