Build and Deploy NodeJs REST API On Firebase Cloud Functions in 5 Minutes !!

David Ezekiel
3 min readSep 29, 2020

--

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

  1. A Firebase Account create one here
  2. NodeJs Installed on your computer download here
  3. 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.

Create Project on Firebase console
Create Project on firebase console
Our project is now ready
Our project is now ready 🤗

Let’s now setup cloud functions on the firebase dashboard.

Setup cloud functions on our project’s dashboard
Setup cloud functions on our project’s dashboard

Now that our project is created and cloud functions are setup let’s jump to the terminal and setup our node project.

  1. Create a directory and run npm init to initialize a new node application.
  2. Inside the the new directory run firebase init to initialize firebase in our application. Select functions in the setup screen, then choose an existing application, choose the application we created from the firebase console, choose JavaScript as the language and finally install dependencies.
  3. Move to the functions directory by running cd functions
  4. Run npm install express cors to install the dependencies that we are going to need.
  5. Add the code below.
index.js code

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 your package.json file to node 8

FYI: 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 !

Our API is now hosted on Firebase Servers and we can test it
Our API is now hosted on Firebase Servers and we can test it

Follow me on GitHub 🤗🤗

--

--

David Ezekiel

Ix Engineer from Nairobi, Kenya Interested in Mobile Application Development, nodeJs and everything IOT.