Firebase Hosting with Dynamic Cloud Features Overwrites

I have an express.js based cloud functions application on firebase in a function named api. To use a custom domain, I'm trying to use Firebase host rewriting to route a specific URL to a function. I follow the official documentation on Firebase cloud features and hosting here, https://firebase.google.com/docs/hosting/functions and have tried many combinations, including the following:

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

"rewrites": [
      {
        "source": "/api/:path1/:dat1/dat",
        "function": "api/:path1/:dat1/dat"
      }
    ]
"rewrites": [
      {
        "source": "/api/path1/dat1/dat",
        "function": "api"
      }
    ]
"rewrites": [
      {
        "source": "/api/*/*/*",
        "function": "api"
      }
    ]

Unfortunately, this does not work for any possible combination. My express application has the following GET paths that I plan to use:

'/api/users/:userId/:userData'
'/api/users/:userId/:userData/json'
'/api/users/:userId/'

and others like them: userId and: userData are parameters in my request, as this works with express.js

https://my-firebase-app.cloudfunctions.net

https://my-app.firebaseapp.com

, , .

EDIT: , .

const functions = require('firebase-functions');
const express = require('express');
const app = express();

app.get('/users/:userId/:userData/json', (req, res) => {
    // Do App stuff here
}
// A couple more app.get in the same format

exports.api = functions.https.onRequest(app);

2: @DougStevenson

firebase.json,

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

, . , , , . ( SO, , ). 404.html index.html , . .

2: , , :

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

- :

app.get('/users/:userId/:userData/json', Foo)

, - , Uri .

+3
2

, , :

{
    "source": "/api",
    "function": "api"
}

https://my-firebase-app.cloudfunctions.net/api/api https://my-firebase-app.cloudfunctions.net/api, . , api .

main, :

const functions = require('firebase-functions');
const express = require('express');
const app = express();

app.get('/users/:userId/:userData/json', (req, res) => {
    // Do App stuff here
}
// A couple more app.get in the same format

// Create "main" function to host all other top-level functions
const main = express();
main.use('/api', app);

exports.main = functions.https.onRequest(main);

main URL-:

{
    "source": "/api/**", // "**" ensures we include paths such as "/api/users/:userId"
    "function": "main"
}

! api https://my-app.firebaseapp.com/api/users/:userId/:userData , .

, https://my-firebase-app.cloudfunctions.net/main/api, . , main, :

const hooks = express();
main.use('/hooks/, hooks);
+12

Firebase Express.

  • rewrite firebase.json.

    {
      "source": "/api/**",
      "function": "api"
    }
    
  • app.use() , URL- .

    const functions = require('firebase-functions');
    const express = require('express');
    
    const API_PREFIX = 'api';
    const app = express();
    
    // Rewrite Firebase hosting requests: /api/:path => /:path
    app.use((req, res, next) => {
        if (req.url.indexOf(`/${API_PREFIX}/`) === 0) {
            req.url = req.url.substring(API_PREFIX.length + 1);
        }
        next();
    });
    
    app.get('/users/:userId/:userData/json', (req, res) => {
        // Do App stuff here
    });
    
    exports[API_PREFIX] = functions.https.onRequest(app);
    
+1

All Articles