How to use query string in firebase rewrite source/regex?

Viewed 101
"rewrites": [
        {
          "regex": "/example/.*",
          "function": "myFunction"
        },
        {
          "source": "**",
          "destination": "/index.html"
        }
]

Suppose my host is https://test.co

So, on hitting the URL- https://test.co/example/example.com/?a=1&b=90 Firebase should invoke myFunction which looks like this:

    app.get(/\/example\/(.+)/, (req, res) => {
   const url=req.params[0]
})

Here req.params contains only example.com and the query strings are stripped off in the node function. req.params returns just example.com/

How do I ensure that query params are not stripped off and I would be able to access them in myFunction?

1 Answers

If you read the getting started documentation for Hosting Rewrites, you will find some specific wording regarding Cloud Functions

Pair Cloud Functions with Firebase Hosting to generate and serve your dynamic content or build REST APIs as microservices. Cloud Functions for Firebase lets you automatically run backend code in response to HTTPS requests.

This indicates that the Functions routes the path to the cloud functions onRequest URL, which should expose properties within your Request object

 - req.method               "POST"
 - req.get('x-myheader')    "123"
 - req.query.foo            "baz"
 - req.body.text            "something"
 - req.rawBody              The raw (unparsed) bytes of the request

you should be able to parse and decode the string present, just make sure you validate all requests one way or another since you are essentially exposing cloud functions to a public URL

Source:

Related