Serverless With Firebase

In simple terms Serverless architecture allows you to deploy and run your application without having manage servers. So you don’t need to worrying about managing the infrastructure, but still in servers. Reduce cost, scalable, quick deployment these are the few advantages of Serverless. AWS LambdaAzure FunctionsGoogle Cloud Functions are the famous serverless providers.

Hello everyone, so today I am going to talk about how to create a Serverless API using Firebase Cloud Functions. When it’s come to the Firebase Cloud Functions is a product of the Google Cloud Platform team and Firebase team.

Whether your code is written in JavaScript or TypeScript it doesn’t matter. Your code is stored in a manage environment by Google Cloud without manage or scale servers. So if you like to know more about Firebase Functions Click Here.

So let’s begin.

Prerequisites

  • First, make sure you have a Firebase Account ;). If you don’t have you can sign up using your Google Account. Click Here to go to the Firebase Console. Make sure you have enabled Billing.
  • Second, you have to install Node.
  • Postman for check out the API.

and I’m going to use Visual Studio Code as my IDE. You can use your preferred IDE.

So Let’s get started.

Step 1

Create a new directory for your application. Then open it using VS code and type the following command in the terminal.

npm init

Ok. Then you need have to install firebase in your application. Type following command to install Firebase tools globally. After installed Firebase tools, you need to log in to Firebase.

npm install -g firebase-tools firebase login
 

Step 2

Now let’s work with Firebase. Type the following command to initialize Firebase in your application.

firebase init

Select Firebase Functions and hit enter. Then it will ask to create a new firebase project or use an existing project. I already create the Firebase project using the console, so I will select the existing project.

Next, select JavaScript for the language and set No for the ESLint. And finally, install dependencies set Yes.

Firebase initialization complete!. Now let’s go to the coding.

Step 3

In your file explorer open the index.js file inside the functions folder.

 

In the index.js type the following code.

const functions = require(‘firebase-functions’);
const admin = require(‘firebase-admin’);
const express = require(‘express’);
const cors = require(‘cors’);
const app = express();
app.use(cors({ origin: true }));

app.get(‘/hello’, (req, res) => {
  return res.status(200).send(‘Hello from Google Cloud Platform’);
});

app.get(‘/author’, (req, res) => {
    return res.status(200).send(‘Pium Sudhara’);
  });

exports.app = functions.https.onRequest(app);

Ok, now our coding part is done. It’s time to deployment part. Type following command to deploy cloud functions.

firebase deploy

If the firebase deploy command does not work you can type the following command.

firebase deploy –only functions “app”

Now our cloud functions successfully deployed into Firebase. Now it’s time to check the API. Open postman and type your function URL and hit enter. (My function URL is “https://us-cenfireapi21.cloudfunctions.net/app”.)

“Hello from Google Cloud Platform”… yes our API was successfully deployed and working. If you change your route into /author it will give the following output.

And also you can check your API without deploying it into the Firebase. You can use Firebase Emulator Suite. Check my article about Firebase Emulator Suite through this link. Firebase Emulator Suite. Advanced tools for developers build and… | by Pium Sudhara | Medium

Enter the following command to start the Firebase Emulator Suite.

npm run serve

This command will open the Firebase Emulator Suite Dashboard. Under the Functions emulator section, you can check your API.

Conclusions

Whether you are you student or a professional Firebase very helpful in your projects. In some of my personal projects, I used Firebase. You can use Firebase Functions to connect with Cloud Firestore or Realtime Database. Creating APIs with Firebase Functions will reduce the code and it will easy to implement the application.

Recent Posts

logo