Skip to main content

How to Deploy a Node.js Backend on Firebase Functions

 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

  1. Install Firebase CLI globally

    npm install -g firebase-tools
  2. You need to sign up Firebase account and create your project on Firebase.
  3. Login to Firebase

    firebase login
  4. Create a new folder and initialize functions

    mkdir my-firebase-backend cd my-firebase-backend firebase init 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:

const functions = require("firebase-functions"); const express = require("express"); const cors = require("cors"); const app = express(); app.use(cors({ origin: true })); // Example API endpoint app.get("/api/hello", (req, res) => { res.json({ message: "Hello from Firebase Functions!" }); }); exports.api = functions.https.onRequest(app);

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:

firebase deploy --only functions

Once deployment finishes, you’ll see a success message like:

Function URL (api): https://us-central1-yourproject.cloudfunctions.net/api

Now open this in your browser:
👉 https://us-central1-yourproject.cloudfunctions.net/api/hello

You should see this response:

{ "message": "Hello from Firebase Functions!" }

Step 4: Test Your Backend API

You can test your deployed backend using:

  • Browser or Postman

  • Frontend fetch call

Example JavaScript call:

fetch("https://us-central1-yourproject.cloudfunctions.net/api/hello") .then(res => res.json()) .then(data => console.log(data.message));

Step 5: Add Environment Variables (Optional)

Store API keys securely in Firebase:

firebase functions:config:set openai.key="YOUR_API_KEY" firebase deploy --only functions

Access them in your code:

const openaiKey = functions.config().openai.key;

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:

firebase functions:log

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:

{ "hosting": { "rewrites": [ { "source": "/api/**", "function": "api" } ] } }

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

Final Thoughts 

Firebase Functions provide a powerful and cost-effective way to run  Node.js backend without managing any servers. Whether you’re building APIs, automations, or entire applications — Firebase makes deployment simple, fast, and scalable.


Node.js deployment, Firebase Functions, serverless backend, Express.js API, Firebase CLI, deploy backend on Firebase, Node.js tutorial, Firebase hosting integration.
Explore Useful Work Tools Read also for frontend deploy: Watch Tutorial for Frontend Deployment:

Comments