READY-TO-USE repo for real-world firebase cloud functions
I am a real fan of firebase services. I love cloud functions because it connects other firebase services together which allow us to create magical things. However, we have to write it in ES5 and it annoyed me a lot. Fortunately, I found this story from James Hegedus explains how to use ES6 in cloud functions. But it is not enough, because I want to use it in dev and production too. So, I made a little changes with some basic knowledge to make hot-reloading and continuous deployment available. I hope this project can save you some time. Feel free to suggest and comment.
Create 2 projects in firebase console (1 for staging, 1 for production)
Install firebase-tools globally
npm install -g firebase-tools
or with yarn
yarn global add firebase-tools
Login firebase in terminal
firebase login
Go to file .firebaserc in this directory, add your project-id
"staging": "<project-id>",
"production": "<project-id>"
or you can use firebase cli
firebase use --add
Run this command to create build directory
yarn && yarn install:packages
** if you install new packages, you need to run this cmd before dev **
Add service account file to ./src/functions/
You can find service account from your project here
After that, change databaseURL to your staging and production project in ./src/functions/service/constant.js
Run this command in console
yarn dev
this will watch file changes in ./src/functions
open another console, then run
yarn serve
** note: recommend to use node v6.11.5, node > 8 may fail ** create firebase emulator which enables local http function test
Make changes in ./src/functions/controllers
Open browser | postman with
http://localhost:5000/<staging-project-id>/us-central1/api
If you want to test functions locally on production services, go to
./src/functions/service/localEnv.js
change 'staging' => 'production'
set up config for 1st time (cloud functions use Google App Engine) by running
yarn set:staging-config
then
yarn set:production-config
deploy to staging project
yarn deploy:staging
or deploy to production project
yarn deploy:production