webbuild infotech

cloude-function

Create function on the firebase cloud

The Firebase cloud function is used to run the backend code. Whenever create the website in Angular, React JS, or VueJS then some API is used where code is implemented in the backend or say server like Nodejs or any other so same way we can implement the backend logic on firebase cloud function and get the response with API.

Your JavaScript or TypeScript code is stored in Google’s cloud and runs in a managed environment. There’s no need to manage and scale your own servers.

I hope you have some knowledge of firebase and if not then first create the project on firebase click and implement the configuration in the project.

So let’s start with the steps and I hope it would be easy to understand

Step 1: Create an Angular project with ng new payment-with-firebase and in the project.
Step 2: Create a project on firebase click and copy the configuration as below image.

congiguration

Step 3: Now let’s go to the environment.ts file of angular and set the configuration from firebase
export const environment = {
  production: false,
  config : {
    apiKey: "API_KEY",
    authDomain: "AUTH_DOMAI",
    databaseURL: "DATABASE_URL",
    projectId: "PROJCT_ID",
    storageBucket: "STORAGE_BUCKET",
    messagingSenderId: "MESSAGING_SENDER_ID",
    appId: "APP_ID",
    measurementId: "MEASUREMENT_ID"
  }
};
Step 4: So we have set up the configuration and now moving on to the firebase cloud function. And for that first Set up Node.js and the Firebase CLI.

We will need a Node.js environment to write functions, and need the Firebase CLI to deploy functions to the Cloud Functions runtime.

After Node.js and npm installed, we need to install the firebase tool globally from the CMD with
npm install -g firebase-tools command as the bellow image.

firebase-tool

it will install globally because we have used the -g and if the command fails, you may need to change the permission.

Step 5: Now let’s Initialize Firebase SDK for Cloud Functions.

Run the firebase login command from the command prompt in the current project directory.

firebase_login

Note: once you press the firebase login command then it will redirect to the browser for login. and if you already login with other users then for logout use the firebase logout command.

Now Run the firebase init functions command

init_firebase

So once you press the above command then it will ask certain questions for initializing an existing project or for cloud function which script you use etc…

After complete successfully the above process the project structure will look as bellow.

myproject
 +- .firebaserc 
 +- firebase.json  
 +- functions/
      +- package.json  
      +- index.js   
      +- node_modules/

function_folder

Step 6: Now import the required modules like firebase-function and firebase-admin as bellow

const functions = require('firebase-functions');
// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

Step 7: After import the module we can use the functionality for creating a cloud function.

got to function/index.js file and create function.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

//hello World
exports.helloWorld = functions.https.onRequest(
(request, response) => {
    functions.logger.info("Hello logs!", {structuredData: true});
    response.send("Hello from Firebase!");
});

In the above example created the helloWorld function to display a simple text response.

so execute the function in local and test in local with firebase emulators: start

emulator
so now copy the URL and test in Postman
(http://localhost:5001/payment-gateway-cc470/us-central1/helloWorld)
testing_url

 

Note: Here is just a Hello world type function created and instead of that you can create any function with logic and based on you will get the response.

Leave a Comment

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