Firebase-tools: Cloud function: /<segment>/** rewrite doesn't work

Created on 10 Jul 2017  ·  3Comments  ·  Source: firebase/firebase-tools

I have the cloud function

const app = express()

app.get('/ping', (req, res) => {
  res.send('pong');
})

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

and it works fine

https://...cloudfunctions.net/api/ping
pong

Then I add following configuration

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

and try to call it from application url and it fails.

https://...firebaseapp.com/api/ping
404

If I use rewrite all calls to my functions like this

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

then it works

https://...firebaseapp.com/ping
pong

Am I missing something here or isn't this scenario supported?

There is the blog writing about this, which uses and segment in rewrite:

https://firebase.googleblog.com/2017/06/serving-dynamic-content-with-cloud.html

Most helpful comment

You need to add /api to the start of your express route. Prefixes aren't
chopped off when URLs are sent to the cloud function.

On Sun, Jul 9, 2017, 11:27 PM roosi notifications@github.com wrote:

I have the cloud function

const app = express()

app.get('/ping', (req, res) => {
res.send('pong');
})

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

and it works fine

https://...cloudfunctions.net/api/ping
pong

Then I add following configuration

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

and try to call it from application url and it fails.

https://...firebaseapp.com/api/ping
404

If I use rewrite all calls to my functions like this

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

then it works

https://...firebaseapp.com/ping
pong

Am I missing something here or isn't this scenario supported?

There is the blog writing about this, which uses and segment in rewrite:

https://firebase.googleblog.com/2017/06/serving-dynamic-content-with-cloud.html
http://url


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/firebase/firebase-tools/issues/392, or mute the
thread
https://github.com/notifications/unsubscribe-auth/AAAD_updmJ5mvK9NFrx5v-a0lQckEhaHks5sMcRogaJpZM4OSaDu
.

All 3 comments

You need to add /api to the start of your express route. Prefixes aren't
chopped off when URLs are sent to the cloud function.

On Sun, Jul 9, 2017, 11:27 PM roosi notifications@github.com wrote:

I have the cloud function

const app = express()

app.get('/ping', (req, res) => {
res.send('pong');
})

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

and it works fine

https://...cloudfunctions.net/api/ping
pong

Then I add following configuration

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

and try to call it from application url and it fails.

https://...firebaseapp.com/api/ping
404

If I use rewrite all calls to my functions like this

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

then it works

https://...firebaseapp.com/ping
pong

Am I missing something here or isn't this scenario supported?

There is the blog writing about this, which uses and segment in rewrite:

https://firebase.googleblog.com/2017/06/serving-dynamic-content-with-cloud.html
http://url


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/firebase/firebase-tools/issues/392, or mute the
thread
https://github.com/notifications/unsubscribe-auth/AAAD_updmJ5mvK9NFrx5v-a0lQckEhaHks5sMcRogaJpZM4OSaDu
.

@roosi I also ran into this issue last week. It's annoying but the best solution I could come up with for a scenario like this is to create a main function which hosts all other functions and rewrite all hosting calls to it. This enables you to call https://...firebaseapp.com/api/ping, for example.

See my answer here for more details:
https://stackoverflow.com/a/45224176/1309346

Was this page helpful?
0 / 5 - 0 ratings