Initializing the Firebase Cloud project

Let's create an empty project directory called cloud-functions. We will run the following command from the newly created cloud -functions directory to initialize the Cloud Functions:

firebase init functions

This command will walk you through a wizard with different steps and will create the necessary files for your projects. It will ask for your preferred language: Javascript or TypeScript. We will go with the Typescript for this sample. It will also ask you whether you want to associate any existing firebase projects or want to create a new project to associate with it. We will select an existing project. It also asks you if you want it to install the required node dependencies. We will say yes so that it installs all the necessary node packages. If you want to manage the dependencies yourself, you can say no to it. The following screenshot shows how the wizard looks:

The final structure will look like this:

Let's understand some of these files specific to Cloud function:

  1. firebase.json: It includes properties of your project. It contains a property called "source", which points to the functions folder for your Cloud function code. If you want to point to some other folder, you change it here. It also includes a property called "predeploy", which essentially contains the command to build and run your code.
  2. .firebaserc: It contains projects that are associated with this directory. It helps you quickly switch between projects.
  3. functions/src/index.ts: This is the main source file where all your Cloud function code will go. By default, a function called helloworld will already be there in this file. However, it is commented out by default.
  4. functions/package.json: Contains NPM dependencies of this project.
If you are a  Windows user, you may have to change the value of the property "predeploy" in your firebase.json file from 
"npm --prefix  $RESOURCE_DIR run build" to 
"npm --prefix %RESOURCE_DIR% run build", as it sometimes gives an error when you try to deploy your function.

Once the setup is complete, we are good to go with the deployment of our first cloud function. For this sample, we will write a simple function call greetUser, which accepts the name of user in the request parameter and show a greeting message in response:

import * as functions from 'firebase-functions';

export const greetUser = functions.https.onRequest((request, response) => {
const name = request.query.name;
response.send("Welcome to Firebase Cloud Function, "+name+"!");
});

First of all, we need to import firebase-functions to make our functions work. Also, note that the Cloud Function is implemented by calling functions.https, which essentially means that we are using an HTTP trigger.

The greetUser() Cloud function is an HTTP endpoint. If you know ExpressJS programming, you must have noted that the syntax resembles ExpressJS endpoint, which executes a function with request and response objects when a user hits the endpoint. Actually, the event handler for an HTTP function listens for the onRequest() event, which supports routers and apps managed by the Express web framework. The response object is used to send response back to the user, in our case, a text message, which the user will see in the browser.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset