This project is a guide to deploying an Express app using Firebase Functions.
- Firebase account
- Firebase CLI installed
- Node.js and npm installed
- MongoDB database
- .env variables
- aws bucket
- Create a Firebase application on the Firebase console and switch to the Blaze plan.
- Initialize the Firebase app from terminal via
firebase init
and choose the functions service. Invokes the CLI which creates the following files :-- functions folder
- .firebaserc
- firebase.json
- Set up your project in the functions folder, which will be our new home. Nothing changes except the fact that we omit the
app.listen
property on the index.js. The rest, including modules, models, routes, controllers, and anything in between, works fine. - The .env variables will be loaded automatically on deployment.
- Ensure you are using the latest npm version while executing the steps above.
- Test your app locally using
firebase serve
. - Deploy using command
firebase deploy
.
- The firebase.json defaults are good.
- If it works locally
node index.js
, then it works via simulationfirebase serve
, and it will definitely work well on deploymentfirebase deploy
. - Remember to delete previous Firebase initializations on your desktop. (This was the main issue I encountered)
- No funny directories or sub-directories should be created.
-
app.listen
is notorious in bringing the address already in use error onfirebase serve
. Get rid of it!
After successful deployment , only authenticated users can access the server , to allow public access:
- Go to console
- Select the function to which you want to give public access
- Click on PERMISSIONS
- Click on ADD PRINCIPAL
- Type allUsers
- Select role Cloud Functions > Cloud Functions Invoker
- Hit Save
Using Firebase Functions makes hosting your backend easy and seamless. The steps outlined above will guide you in deploying your Express app using Firebase. Ensure you have stable internet; it might be the cause of some funny errors between simulation and deployment.