Software DevelopmentCreating a “Serverless” API using “Firebase Cloud Function”

Creating a “Serverless” API using “Firebase Cloud Function”

What are Firebase Cloud Functions?
Cloud Functions for Firebase are used in order to run background code automatically which only responses to the events that are triggered by Firebase features as well as HTTPS requests. The most amazing thing that happens by using Firebase Cloud Function is, that the codes that you write are stored in Google’s Cloud so that they run in an automatically managed environment. So there is no need to manage your own servers.
The Cloud function of the Firebase works on the basis of some Triggers, the cloud functions can be triggered by making changes in the database, storage, analytics, signups etc. The firebase also has an ability to make a function HTTP triggered. We have listed some triggers that are that proves the ability of cloud functions:

  • Real-time database triggers: These kinds of triggers can be joined to any hub of the firebase continuous no-SQL, JSON database. For each adjustment in that hub, the capacity is activated
  • Firebase Analytics triggers: Analytics for Firebase gives occasion reports that assistance you see how clients interface with your application. With Cloud Functions, you can get to transformation occasions you have logged and trigger capacities in light of those occasions.
  • Firebase Authentication triggers: firebase additionally gives an authentication SDK that empowers application designers to coordinate social login with significant character suppliers like Facebook, Google, Twitter and so forth
  • Cloud storage triggers: You can trigger a function in reaction to the uploading, updating, or deleting the files and folders in Cloud Storage
  • HTTP Triggers: This cloud work is simply playing out some arbitrary undertaking in light of the demand. Like, reject PUT asks for, return client check from the ongoing database with or without the name parameter that gets going in the post asks.

Triggers

What is Serverless?

In basic terms, Serverless engineering is a programming worldview that focuses on coding capacities for web-endpoints honestly instead of experiencing the entire procedure of setting up a server, steering demands, characterizing REST assets and afterward making techniques for getting, POST and PUT

The term which is given for this whole process is Serverless computing which works on the basis of Function as a Service (FaaS) it is a cloud computing code executor which is managed by the cloud provider like starting and stopping the function when it’s necessary.

Customarily, we’ve constructed and conveyed web applications where we have some level of control over the HTTP asks for that are made to our server. Our application keeps running on that server and we are in charge of provisioning and dealing with the assets for it.

Requirements for building Serverless App using Firebase Cloud Functions

1. Basic knowledge of JavaScript language.

2. A Firebase Account.

3. Installed Node.js

4. Libraries: cors, axios

5. Postman (Used for the testing purpose of API endpoints)

Creating the Firebase Project

In order to create a firebase project, you will have to follow a few steps mentioned below.

1. Visit https://firebase.google.com

2. Then log in to Firebase using your Google account, in case you don’t have a Google account then create one.

3. Then Go To Console

Go To Console

4. After that click the button Add Project

Add Project

5. Give a name to your project and Click the Create Project button

Create Project button

6. Now, after the creation of the project, you will be redirected to the Firebase Console.

7. Also, there is a need to set up the Firebase locally on the device to integrate it with the application.

Initialize the Project

Now, the project will get initiated, and in order to do so, we will have to authenticate the Firebase in the device.

Firebase Login – Run this command firebase login and authenticate the firebase tool.

Then some questions will be asked to you, answer those questions after the successful login into Firebase Functions.

Firebase inits functions – Now, Run Firebase ‘init’ Function command to initiate.
Now, a list of all your projects will open, and you will have to choose the project you have just created.

Choose JavaScript as the language to write Cloud Functions.

Don’t use ESLint to catch bugs and enforce style.

Now, install the dependencies with npm. Now, your project is going to set up as well as it will install all your dependencies.

After the completion of installation, open the project folder, after clicking on the project folder you will see some options like shown below.

Firebase inits functions

Creating the Endpoint

The endpoint is created by defining the function which is used to expose a URL in the cloud.
The first thing that needs to be done before creating the first endpoint is changing the function folder of the directory.
Then open the index.js file and insert the code given below.
endpoint

After inserting the code snippet the app needs to be deployed.

Deployment of the Firebase

To deploy the app run any of the following commands in the terminal.
Firebase deploy
Or
Firebase deploy –only functions: HelloWorld

Now, the app will be successfully deployed and a URL will be generated.

To test the generated URL, copy the URL and paste it in the browser’s address bar and the resultant URL should look like this:

If you see so, then Congratulations you succeed in the creation of the Endpoint.

Pre Setup

1. Enabling CORS
As a matter of course, our server won’t acknowledge asking for from any open substance and will deny access to any all-inclusive JavaScript/program, aside from we empower CORS.
CORS implies Cross-Origin Resource Sharing and it’s a determination that empowers genuinely open access crosswise over area limits. CORS presents a standard instrument that can be utilized by all programs for actualizing cross-space demands.

In order to enable CORS, it is installed and configured. Open the Terminal and insert the following command
npm i -S cors
Then, import and configure it
Const cors = require (‘cors’) ({ origin:true });

2. Setup the Database

Before we make the URL where the information will be continued to, the database way must be designed.

Setup the Database

3. Performing Crude Operations

After enabling the database the next step that comes is to “perform Crude Operations”, where we will show you that the user is adding the item to the list.

perform Crude Operations

Deploy Firebase

Now, the last thing is Deploying the Firebase, The command is capable of deploying multiple URL’s. The URL’s can be tested with the POSTMAN. And the final URL that you will get should be like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -