How to deploy NodeJS + Express app to Firebase as Function?

Sandun Isuru Niraj
5 min readApr 26, 2019

This article is going to talk about deploying NodeJS REST API, composed with Express framework to the Firebase as Firebase Function.

Firebase recently launched Firebase functions apart from their Google Cloud functions to help developers to develop applications using their Firebase Real-time database, Firebase Storage, Firebase Authentication and other functionalities.

We all know apart from Google, there more providers give solutions as Firebase functions. AWS Lambda which was introduced by Amazon Web Services is one of the popular services. Like that Firebase also has a simple mechanism to reduce the gap between development and deployment.

Before proceeding you need to know these things. Firebase has some pricing plan.

Also they are saying Free Spark plan has only access to the Google Services. Therefore you can’t use Spark plan to connect with external APIs. So you need to upgrade to the Flame plan or Blaze plan. How ever Blaze plan supplies some limits as free of charge.

Free Allocation of Blaze Plan

So If you considering testing and educational purposes, Using the Blaze plan is the best solution.

OK, Let’s we consider about how we deploy Express app as function to Firebase functions.

First, I’ll show which app we are going to deploy.

Sample program.

This is a simple app that consists of one API endpoint with GET method. you can run this and give output like this.

Postman output

I don’t think you need to explain the app that I have wrote. So then we move to the deployment phase.

Then you need a Firebase account and Firebase Project. So you can create Firebase account by logging in to console.firebase.com.

Then you need to create Firebase Project

Click on Add Project.

Now enter the project name and tick on the I accept… then Create Project button. Then It will complete creating your project shortly.

Then you need to install Firebase CLI globally to continue. Type this command on Command Prompt or Terminal

npm install -g firebase-tools

This will install Firebase CLI on your PC.

Now you need to login to your Firebase account using the Firebase CLI. Use firebase login command.

Then enter y and press Enter button. This will pop up a browser window and you can login through it.

Firebase Login through Firebase CLI

Now you have logged to the Firebase through the CLI. Then we can continue other work.

Then, enter firebase init to initialize new Firebase deployment.

It will ask permissions to proceed. Proceed by click enter.

Now you need to select the service that you need. You can navigate among the options using the arrow keys and select options using the Space button. So in here select the Functions option and press Enter.

Then, Select the project you need to deploy the Function and press Enter.

Now select the language you prefer and press Enter. In this project,I have chosen the Javascript.

Now It will ask the installing relevant dependencies. Press Enter to proceed and It will takes some time.

Now It is completed installation.

Now you can see a folder called functions and switch to that folder. You can see this type of folder structure.

Now, Open that index.js file in your favorite editor.

You can see this type of file. Now we need to add our code to this file.

First, we need to add expree to that project. Run npm install express --save to add express to project.

Then copy the code below the

const functions = require(‘firebase-functions’);

Code Fragment.

Like this.

Then add these code fragment to after the above code.

exports.app = functions.https.onRequest(app);

Now our coding part is completed. Now we can check Is this work as Firebase Function correctly.

Run firebase serve Command in the function folder.

Then you can check if this work as function by sending request to the link that marked by a blue arrow.

If it is working, We need another small configuration. Firebase Functions are still working for Node 6(Now it is too old.) and also they supply beta for Node 8. I have developed this using Node 10. But I have not found any method to run this inside the Firebase using Node 10. But fortunately, my Node 10 app worked in Node 8 environment. So If you deploy Node 8+ compatible app. Add this attribute to the package.json file.

“engines”: { “node”: “8” }

Now you are ready for the deployment.

Now inside the functions folder enter this command

firebase deploy

If you all the things correctly. This will show to you and That link showed by Blue arrow is the function URL.

In the Firebase console, you can see the deployed function.

Now you can check deployed API endpoint using Postman.

You can see this output.

OK, I think you have a clear understanding of Firebase functions and their deployment. If you have any kind of problem, the comment section is yours.

Good Luck….!

--

--