Skip to content

Express middleware in Firebase cloud functions.

Firebase cloud functions are the really helpful tool for mobile developers which lets them build Android/IOS apps without having the ease of maintaining a server. Cloud functions have heavily relied on famous Javascript framework called Node.Js. It’s event-driven and non-blocking I/O model architecture makes it more efficient and lightweight. You can easily trigger HTTP functions in firebase cloud functions using the node.js environment.

Firebase logo,But for a production-ready application which has many HTTP function triggers, Maintaining identity of every HTTP requests is a pain point. For example, You may have to check the existence or state of the requesting user/account for all the HTTP requests. This is the plot where middlewares come into action for you. In simpler terms, Middleware is a framework/bridge which connects or performs some functions in between the client and server. A middleware can process the request and response as well as a server does. Express is a web application framework developed for Node.js which can be maintained as a middleware in Firebase cloud functions.This tutorial will help you to implement a simple middleware application using express web application framework. This is a beginner friendly tutorial to the Firebase Cloud Functions. First, Setup a firebase cloud function app on your machine as described in this official document.

After setting up the Firebase CLI and Cloud functions project directory, You can head up to into the index.js file of functions folder in root directory of the project.As you go through the official document, You can see that setting up an endpoint and it’s handler functions is easy to implement and push it to the original server. In below snippet, I have created a function which returns a sample JSON response to the requesting client.

Okay, Now everything works fine! I get a sample response in JSON format when the client Android app makes the call. Now What If I need to validate the client’s identity before I make the response? The function should not trigger when a client is a trespasser. So I should call a validate function before the actual function right? This is where express.JS middleware comes into help for in your Firebase cloud function project.

Okay, let’s start implementing express js. Initially, as like an Android project, we have to add a dependency of express on our project. For that, head to root/functions/package.json file. This is where you should declare app dependencies. Add the dependency same as like this.

"dependencies": {
    "firebase-admin": "~5.8.1",
    "express": "^4.15.2",
    "firebase-functions": "^0.8.1"

Or else, You can simply run the below command in your functions folder.

yarn add express

We have completed our initial setups now let’s get into the code.
In your index.js file initiate express instance below like this.

const express = require('express');

And create the app instance ,

const app = express();

Now here comes the magic! Create a function called validate() which is a constant with parameters of the request, response and next. next() simply allows the next route handler in line to handle the request.It passes control to the next matching route.
After the client validation in middleware, next() will take the request to the actual function or else, It will throw error response to the user. You can implement your DB related queries for searching the user inside this function scope.

Now we have to make use of this middleware function with the app instance. So call


Now let’s define our routed or actual endpoint. We can use any of app.all(), app.get(),, app.delete() and app.put() methods . All those GET,POST,DELETE,PUT annotations of retrofit library call is recieved by these functions.

Last but not least, We should make the request to Firebase CF to make use of our middleware in the project by adding the below line at the end of the index.js file.


Finally, Deploy our project. Your endpoint of contact from the client app will be
BASE_URL/api/hello/ for this example. This is how you can easily implement a middleware in Firebase cloud functions.

Happy coding!

Published inFirebase


  1. Hey! Would you mind if I share your blog with my
    myspace group? There’s a lot of folks that I think would really enjoy
    your content. Please let me know. Thank you

    • Yes you can buddy 🙂

  2. My cousin advised I may like that blog site. He was previously totally suitable.. godaddy reseller This informative article definitely created this time. You can not consider exactly how much period I had put together spent with this information and facts! Cheers!

  3. Fine way of explaining, and pleasant piece of writing to obtain information on the
    topic of my presentation topic, which i am going to present in academy.

  4. Simply want to say your article is as astonishing.
    The clarity to your publish is just great and that i could suppose you
    are an expert in this subject. Fine together with your
    permission let me to snatch your RSS feed to stay updated
    with approaching post. Thank you 1,000,000 and please keep up the gratifying

  5. This site was… how do you say it? Relevant!! Finally I have found something
    which helped me. Cheers!

  6. Great beat ! I would like to apprentice whilst you
    amend your website, how could i subscribe for a blog website?

    The account helped me a appropriate deal. I
    have been tiny bit familiar of this your broadcast provided vivid transparent

  7. Good day! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in exchanging links or maybe
    guest writing a blog post or vice-versa? My site goes over a lot of the same subjects as
    yours and I think we could greatly benefit from each other.
    If you are interested feel free to send me an email.
    I look forward to hearing from you! Excellent blog by the

  8. Hey I am so glad I found your weblog, I really found you by mistake, while I was browsing on Google for something
    else, Anyhow I am here now and would just like to say
    thanks for a remarkable post and a all round enjoyable blog (I also love the
    theme/design), I don’t have time to go through it all at
    the minute but I have book-marked it and also added in your RSS feeds,
    so when I have time I will be back to read a great deal
    more, Please do keep up the fantastic job.

  9. I am sure this article has touched all the internet visitors, its
    really really nice paragraph on building up new website.

  10. It’s actually a nice and helpful piece of info. I am glad that you
    just shared this useful information with us. Please stay us
    up to date like this. Thanks for sharing.

  11. This site really has all of the info I wanted about this subject and didn’t know who to ask.

  12. Great post. I was checking continuously this blog and I am impressed!
    Very useful information specially the last part 🙂 I care for such information a lot.
    I was seeking this certain information for a
    long time. Thank you and good luck.

  13. Thank you for any other magnificent post.
    Where else may anybody get that type of info in such an ideal manner of writing?
    I’ve a presentation next week, and I am on the
    search for such information.

  14. Hmm it appears like your website ate my first comment (it was super
    long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying
    your blog. I as well am an aspiring blog writer but I’m still new to
    the whole thing. Do you have any recommendations for beginner blog writers?
    I’d really appreciate it.

  15. Some truly interesting info , well written and generally user genial.

Leave a Reply

Your email address will not be published. Required fields are marked *