Express.js Demo for CC-25
🇮🇩 Part of Bangkit 2022
🛑 Please use it wisely! Remeber Bangkit 2022 Code of Conduct! Use it as reference only!
Stack used
- 🟨 Express.js Web Framework
- 💾 MySQL 8.0
Google Cloud Service used
- 💾 Cloud Storage
- ⚡️ Cloud Run
- 🖥 Compute Engine
- 🤖 Vertex AI
Local requirements
- 🟨 Node.js
- 🐳 Docker
- 🚦 Any IDE you like
👨🏽💻 How to use in local
- Clone this repositroy
- Run
npm install
- Create
.env
file in root folder with this specifications
Variables | Function | Example |
---|---|---|
PORT | your_API_port | 8080 |
TOKEN_KEY | your_bcrypt_auth_token | YVTiYss7HOU= |
MYSQL_ROOT_USER | your_mysql_root_user | root |
MYSQL_ROOT_PASSWORD | your_mysql_root_pass | passw0rd |
MYSQL_HOST | your_mysql_host_address | 0.0.0.0 |
ML_API_ENDPOINT | your_machine_learning_endpoint | https://endpoint.com |
- Run
docker run --name expressjs-demo-mysql -e MYSQL_ROOT_PASSWORD=passw0rd -p 0.0.0.0:3306:3306 -d mysql:8.0
- Run
docker exec -it expressjs-demo-mysql bash
- Populate Database with
init_mysql.sql
by:- Run
sudo mysql -p
- Type your password same as
MYSQL_ROOT_PASSWORD
- Copy paste all sql script in
init_mysql.sql
- Exit mysql cli with
exit
- Run
- Exit docker with
exit
- Run application with
npm run dev
☁️ How to use in Cloud
⚡️ Deploy MySQL Database in Compute Engine
- Use this link to deploy Virtual Machine with MySQL automatically installed
- Click Launch and Enable API Needed
- In Deployment page, setup your Virtual Machine
- Make sure to checklist
Allow TCP port ...
so you can test your database from local
- Checklist Term of Services
- Deploy and wait
- 📝 Note your MySQL user and password
- SSH to Virtual Machine
- Run
sudo apt update && sudo apt install git
- Clone this repository
- Run
cd expressjs-demo
- Run
sudo mysql -p < init_mysql.sql
- Type your password
- Done. You can exit SSH
- Go to GCP Console > Compute Engine
- Click your MySQL Virtual Machine
- Stop it
- Click Edit
- In
Network Interface
, click the dropdown
- In
External IPv4 address
, click the dropdown and clickCreate IP Address
- Give your IP name and click
Reverse
- Click Done and then Save
- Wait and start your Virtual Machine again
- 📝 Note your external IP address
⚡️ Deploy API into Cloud Run
- In file
.env
changes yourMYSQL_ROOT_USER
,MYSQL_ROOT_PASSWORD
, andMYSQL_HOST
with your 📝 Note from step 7 and 24 - Run
export PROJECT_ID=$(gcloud config get-value project)
- Run
docker build -t gcr.io/$PROJECT_ID/expressjs-demo:v1.0 .
- Run
gcloud auth configure-docker
and pressY
- Run
docker push gcr.io/$PROJECT_ID/expressjs-demo:v1.0
- Run
gcloud run deploy --image gcr.io/$PROJECT_ID/expressjs-demo:v1.0 --platform managed
- Check your GCP Console > Cloud Run
References :
[1] https://medium.com/@rahulguptalive/create-crud-apis-in-nodejs-express-and-mysql-abda4dfc2d6
[3] https://stackoverflow.com/questions/40076638/compare-passwords-bcryptjs