Node JS Express + Firebase: A Boilerplate
Firebase by Google is a trusted solution for managing a back end that is like the Swiss Army Knife of backend solutions - to me personally, it is one of the ultimate tool that every developer needs in their toolkit. It offers authorisation management that is just unbelievably simple to set up and a robust NoSQL database called Firestore that simplifies database management with its user-friendly UI and easy to understand NoSQL approach among its star features.
It is of course the back end of choice for a majority of start ups that I talked to and a major choice to use if one was to build an minimum viable product (MVP, in colloquial speak). But of course using Firebase, you must adhere to strict rules governed in its documentation. Is there a way to use our own back end, maintaining Firebase as a service that provides authentication and, in this case, database privileges determined by our own custom build back end, maybe with a NodeJS Express implementation perhaps.
The question here to put it succinctly; can Firebase and NodeJS Express implementation coexist like two peas in a pod, or will they clash like oil and water? Well, if you’ve been searching for an answer to this conundrum, your search ends here.
This article aims to provide, in some way, a NodeJS Express with Firebase implementation that serves as a helpful reference not only to me, the writer, but also to anyone who wants to learn or refresh their knowledge of the framework.
While the implementation is presented with the intention of providing a reminder to me to refer to in the future, this article has been written to be accessible and understandable for NodeJS Express devs who wants to take advantage of using their own server implementation with Firebase.
Hopefully, this article serves as an excellent boilerplate and reference point for anyone looking to expand their understanding of both Firebase and NodeJS Express features and capabilities and how to integrate the two without breaking their head reading through the extensive documentation.
Pre requisite- because I will not be covering everything in this article
A. Mediocre level knowledge in NodeJS and Express
B. Firebase account and knowledge on how to use Firebase
Step 0: Setup Firebase and a Node JS Express project
A. Start a Firebase account. This is quite simple, just head over to firebase.google.com to get started
B. Start a Node JS Express project, check out the following website to get started
Step 1: Setup service account and download credential file in Firebase
A. Get Service Account file
Click on the gear icon next to Project Overview to get into settings
So, in order to continue on with your amazing project, you’ll need to make your way over to the ‘Project Settings’ section. Once there, you’ll want to navigate on over to the ‘Service Account’ tab.
Here’s where the real fun begins — just click on the ‘Create Service Account’ button and you’ll have the all-important ‘Service Account’ file downloaded right into your computer.
B. Get Web API Key
You will also need to your hands on the Web API Key. From the same menu, select the ‘General’ tab and copy down the Web API Key somewhere. You will need this later in this article, just keep it close.
Step 2: Place credential in ./cred folder in the NodeJS Express project
A. Create a cred folder in NodeJS Express root and place the generated service account file in the cred folder. Just place the service account file in the cred folder.
B. Create a simple variable holding file for the web API key in the cred folder. Let’s name this file, the apikey.js file. Here’s the code:
1 const apiKey = 'AIza<whatever your key is>fsksdjfcbewrASD';
2
3 exports.apiKey = apiKey;