Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud functions routing

How to process paths like /user/:id and /user/:id/report in HTTP Trigger Firebase cloud functions with access to param id ?

I didn't like next solution because all my functions would be part of the one with prefix /api :

const app = require('express')();
exports.api= functions.https.onRequest(app);
app.get('/user/:uid/report', (req, res) => {}):

But i need something like that:

exports['/user/:uid/report'] = functions.https.onRequest((req, res) => {
   // ...
});

UPDATE: It's impossible to have wildcards in hosting routes and get params from it

like image 484
dubace Avatar asked Dec 18 '22 03:12

dubace


1 Answers

You can use Firebase Hosting on top of Cloud Functions for Firebase to rewrite URLs to suit the path you want. This means you'll have to firebase init again and add Hosting if it's not already.

In your project firebase.json you'll have to add a function rewrite to send all requests to hosting /** to function api:

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

When you deploy, you'll be given a hosting URL in the output. Use this instead of your function URL.

And now, with Hosting in front of Functions, you have to ability to set up caching.

like image 105
Doug Stevenson Avatar answered Dec 20 '22 18:12

Doug Stevenson