Build and Deploy NodeJs REST API On Firebase Cloud Functions in 5 Minutes !!
In this tutorial We are going to build and deploy a NodeJS REST API on Firebase Cloud Functions in 5 or so Minutes 🥳🥳
For this tutorial We’ll be using ExpressJs as our Framework.
Prerequisites
- A Firebase Account create one here
- NodeJs Installed on your computer download here
- Firebase Tools Cli which you can install globally by running
npm install -g firebase-tools
Project Setup
First we are going to need a firebase project so let’s create one on the firebase console.
Let’s now setup cloud functions on the firebase dashboard.
Now that our project is created and cloud functions are setup let’s jump to the terminal and setup our node project.
- Create a directory and run
npm init
to initialize a new node application. - Inside the the new directory run
firebase init
to initialize firebase in our application.Select functions
in the setup screen, thenchoose an existing application
, choose the application we created from the firebase console,choose JavaScript
as the language and finallyinstall dependencies
. - Move to the functions directory by running
cd functions
- Run
npm install express cors
to install the dependencies that we are going to need. - Add the code below.
If we want to test our api locally before deploying to firebase we can run firebase serve
in our functions folder and get a url like this http://localhost:5000/noderestapi-3ffc1/us-central1/FruitsApi
To deploy our function we run firebase deploy —-only functions
and that’s it were done ! 🤩🤩 We should get a url like this once we deploy our API https://us-central1-noderestapi-3ffc1.cloudfunctions.net/FruitsApi
Note: incase you get such an error
HTTP Error: 400, Billing account for project ‘375158879459’ is not found. Billing must be enabled for activation of service(s) ‘cloudbuild.googleapis.com,containerregistry.googleapis.com to proceed.
You may want to change the node runtime in yourpackage.json
file to node 8FYI: Firebase will stop supporting node.js runtime 8 from 15/03/2021. In order to use Node runtime 10 you will need to enable billing for your project.
We are now ready to test our api !
Follow me on GitHub 🤗🤗