Integrating Firebase into the backend
Firebase is already a complete solution that can simply replace our backend, but sometimes, due to some requirements, you will find yourself integrating Firebase into the already present backend.
In this integration process, we will integrate Firebase services in a NodeJS backend application.
How to do it...
Since we're using NodeJS, integrating Firebase is one module setup away:
- Head directly to your terminal (cmd on Windows) and write down the following command:
~ cd project-directory ~/project-directory ~> npm install firebase --save
Now, the preceding command will go and download Firebase locally so you can access it directly using your normal commonJS
workflow.
- Now, you will need to grab yourself a copy of your Firebase project configuration. This step is relatively easy as you can find the required configuration metadata by following the steps mentioned in the previous section, Adding Firebase to an existing frontend project, where we introduced how we can add Firebase to a frontend project.
- Head directly to your favorite code editor and write down the following:
// [*] 1: requiring/importing Firebase in our workflow const firebase = require('firebase'); // [*] 2:Initialising our application with our credentials var config = { apiKey: "<API_KEY>", authDomain: "<PROJECT_ID>.firebaseapp.com", databaseURL: "https://<DATABASE_NAME>.firebaseio.com", storageBucket: "<BUCKET>.appspot.com", }; firebase.initializeApp(config);
Congratulations, you've successfully integrated Firebase within your backend workflow. I also want to point out that we can extend the workflow we have now using something called Firebase Admin SDK. We will cover how we integrate it and work with it in Chapter 7, Firebase Admin SDK.
How it works...
Similar to the frontend integration, in our backend we are doing the following:
- Using a node package manager or npm to install the Firebase
commonJS
library, where we will find all the necessary APIs. - We're requiring/importing Firebase to be part our application, passing it to the configuration object that will hold all our API keys, links, and more.
- Lastly, we're initializing our application with the configuration object we just created.