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.
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 app.use(validateFirebaseRequest);
Now let’s define our routed or actual endpoint. We can use any of app.all(), app.get(), app.post(), 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.