If you are building server-rendered Angular or Next.js apps, check out next-generationFirebase App Hosting,a full-stack solution for modern web frameworks.
Serve dynamic content and host microservices with Cloud Functions
Stay organized with collectionsSave and categorize content based on your preferences.
PairCloud FunctionswithFirebase Hostingto generate and serve your
dynamic content or build REST APIs as microservices.
Cloud Functions for Firebaselets you automatically run backend
code in response to HTTPS requests. Your code is stored in Google's cloud and
runs in a managed environment. There's no need to manage and scale your own
servers.
For example use cases and samples forCloud Functionsintegrated withFirebase Hosting, visit ourserverless overview.
ConnectCloud FunctionstoFirebase Hosting
This section provides a walk-through example for connecting a function toFirebase Hosting.
Note that to improve the performance of serving dynamic content, you can
optionally tune yourcache settings.
Step 1:Set upCloud Functions
Make sure that you have the latest version of theFirebaseCLI and that
you've initializedFirebase Hosting.
For detailed instructions about installing the CLI and initializingHosting, see theGet Started guide forHosting.
InitializeCloud Functionsby running the following command from
the root of your project directory:
firebase init functions
When prompted, select JavaScript (this walk-through example uses JS).
Check that you have afunctionsdirectory in your local project
directory (created by the Firebase command you just ran). Thisfunctionsdirectory is where the code forCloud Functionslives.
Step 2:Create and test an HTTPS function for yourHostingsite
Open/functions/index.jsin your favorite editor.
Replace the file's contents with the following code.
This code creates an HTTPS function (namedbigben) that replies to HTTPS
requests with aBONGfor each hour of the day, just like a clock.
The next step walks you through how to access this HTTPS functionfrom aFirebase HostingURLso that it can generate dynamic content for your
Firebase-hosted site.
Step 3:Direct HTTPS requests to your function
Withrewrite rules, you can direct requests
that match specific patterns to a single destination. The following steps show
you how to direct all requests from the path../bigbenon yourHostingsite to execute thebigbenfunction.
Add the followingrewriteconfiguration under thehostingsection:
"hosting":{// ...// Add the "rewrites" attribute within "hosting""rewrites":[{"source":"/bigben","function":{"functionId":"bigben","region":"us-central1"// optional (see note below)"pinTag":true// optional (see note below)}}]}
Confirm that your redirect works as expected by testing again with the
Firebase emulators.
From the root of your local project directory, run the following
command:
firebase emulators:start
Visit the locally hosted URL for your site as returned by the CLI
(usuallylocalhost:5000), but append the URL withbigben, like so:http://localhost:5000/bigben
Iterate on your function and its functionality for your site. Use the
Firebase emulators to test these iterations.
Howregionworks within thefunctionblock
Ifregionis omitted from afunctionblock of thehosting.rewritesconfig, theFirebaseCLI attempts to automatically detect the region from
the function's source code which, if unspecified, defaults tous-central1.
If the function's source code is unavailable, the CLI attempts to detect
the region from the deployed function. If the function is in multiple regions,
the CLI requiresregionto be specified in thehosting.rewritesconfig.
HowpinTagworks within thefunctionblock
ThepinTagfeature is only available inCloud Functions for Firebase(2nd gen).
With this feature, you can ensure that each function for generating your
site's dynamic content is kept in sync with your staticHostingresources
andHostingconfig. Also, this feature allows you to preview your rewrites
to functions onHostingpreview channels.
If you add"pinTag": trueto afunctionblock of thehosting.rewritesconfig, then the "pinned" function will be deployed along with your staticHostingresources and configuration, even when runningfirebase deploy --only hosting. If you roll back a
version of your site, the "pinned" function is also rolled back.
This feature relies onCloud Runtags,
which have a limit of 1000 tags per service and 2000 tags per region. This
means that after hundreds of deploys, the oldest versions of a site may stop
working.
For the best performance, colocate your functions withHostingby
choosing one of the following regions:
Note that to improve the performance of serving dynamic content, you can
optionally tune yourcache settings.
Step 4:Deploy your function
Once your function is working as desired in the emulator, you can proceed to
deploying, testing, and running it withrealproject resources. This is a
good time to consider setting runtime options tocontrol scaling behaviorfor functions running in production.
Deploy your function as well as yourHostingcontent and config to your
site by running the following command from the root of your local project
directory:
firebase deploy --only functions,hosting
Access your live site and your function at the following URLs:
Your Firebase subdomains: PROJECT_ID.web.app/bigbenandPROJECT_ID.firebaseapp.com/bigben
In yourfirebase.jsonfile, direct all requests to theappfunction.
This rewrite allows Express.js to serve the different subpath that we
configured (in this example,/and/api).
Continuing our example, now that you're using Express.js, you can addExpress.js middlewarein the typical way. For example, you can enableCORSrequests on our endpoints.
Install thecorsmiddleware by running the following command:
npm install --save cors
Open your/functions/index.jsfile, then addcorsto your Express.js app,
like so:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,[]]