Learn how to deploy a Node.js backend on Firebase Cloud Functions step-by-step. Includes setup, Firebase CLI commands, and deployment guide for production.
Introduction
Building a backend for your web or mobile application doesn’t have to be difficult or expensive.
With Firebase Cloud Functions, you can host and deploy a Node.js backend directly on Google Cloud — no need to manage servers or infrastructure.
In this blog, you’ll learn:
-
How to set up a Node.js backend using Firebase
-
How to deploy Express APIs with Firebase Functions
-
How to test your deployed backend easily
Step 1: Install Firebase CLI and Initialize Project
-
Install Firebase CLI globally
- You need to sign up Firebase account and create your project on Firebase.
-
Login to Firebase
-
Create a new folder and initialize functions
👉 During setup:
-
Choose JavaScript (or TypeScript if you prefer)
-
Enable ESLint (recommended)
-
Say Yes to install dependencies
Firebase will create a folder named functions/ with an index.js file for your backend.
Step 2: Write Your Node.js Backend Using Express
Open the file functions/index.js and replace its content with this:
Explanation:
-
Uses Express.js to handle API routes
-
Adds CORS for frontend requests
-
Deploys your app as an HTTPS endpoint called
/api Install required lib:
npm install mysql2 (for MySQL database)
npm install express
npm install cors
npm install axos
npm install
Step 3: Deploy to Firebase
Run this command from your project root folder:
Once deployment finishes, you’ll see a success message like:
Now open this in your browser:
👉 https://us-central1-yourproject.cloudfunctions.net/api/hello
You should see this response:
Step 4: Test Your Backend API
You can test your deployed backend using:
-
Browser or Postman
-
Frontend fetch call
Example JavaScript call:
Step 5: Add Environment Variables (Optional)
Store API keys securely in Firebase:
Access them in your code:
Step 6: Monitor and Manage Your Functions
You can monitor everything from the Firebase Console → Functions tab:
-
View logs
-
Check usage metrics
-
Troubleshoot errors
Or from the terminal:
Step 7: Connect Custom Domain (Optional)
If you’re hosting your frontend with Firebase Hosting, you can integrate both backend and frontend easily.
Add this to your firebase.json file:
Now your API works under your custom domain:
👉 https://yourdomain.com/api/hello
Summary
You’ve successfully deployed a Node.js backend on Firebase Cloud Functions!
It’s serverless, scalable, and automatically maintained by Google Cloud.
Key takeaways:
-
Initialize Firebase with
firebase init functions -
Use Express.js for backend routes
-
Deploy easily using
firebase deploy --only functions -
Test and monitor through Firebase Console

Comments
Post a Comment