Functions-samples: Unable to route the to function when express is used

Created on 1 Apr 2017  路  4Comments  路  Source: firebase/functions-samples

How to reproduce these conditions

Firebase deploy the function below:

```js'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

const express = require('express');
const cors = require('cors');

const app = express();

app.use(cors({origin: true}));
app.get('*', (req, res) => {
return res
.type('application/manifest+json')
.status(200)
.send(JSON.stringify({
message: 'OK',
}, null, 2));
});

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

```

Debug output

  • Firebase logs: Function execution took 235 ms, finished with status code: 404
  • Result on browser when visiting the endpoint: Cannot GET null

Expected behavior

Route to the Firebase's Cloud Function and returns { message: 'OK' } as a result.

Actual behavior

Cannot GET null is display when visiting the endpoint and a trailing slash is needed to run it properly.

Most helpful comment

It seems you'll have to add a trailing / at the URL you are requesting:

https://....cloudfunctions.net/testCloudFunction => 404
https://....cloudfunctions.net/testCloudFunction/ => 200

I haven't investigated further. There might be some special ExpressJS settings needed to support the / or an issue on our side.

All 4 comments

It seems you'll have to add a trailing / at the URL you are requesting:

https://....cloudfunctions.net/testCloudFunction => 404
https://....cloudfunctions.net/testCloudFunction/ => 200

I haven't investigated further. There might be some special ExpressJS settings needed to support the / or an issue on our side.

@nicolasgarnier Yup. A trailing / is needed when writing an Express app.

This looks weird when the endpoint itself accepts query parameters and it needs to be something like this https://...cloudfunctions.net/testCloudFunction/?q=some+text.

One of the examples does something like this too but I don't think it will work as expected without a trailing /.

Hi @motss, I don't believe this is the best way to use express middleware with Functions. See use middleware with Cloud Functions in the docs.

I'm fairly sure that you're creating and running a second instance of express, rather than taking advantage of the one already instantiated by Functions. I'd imagine this could create some weirdness later. The docs have this example, which I'm guessing is intended to be used inside the Function, but I'm quite interested in hearing more, as the doc example assumes we already know how this all works : )

// Enable CORS using the `cors` express middleware.
cors(req, res, () => {
  // ...
});

Also, this issue doesn't follow the issue template and doesn't include a relevant sample link. How does this question relate to the existing samples? If it doesn't, it's probably a question best asked on Stack Overflow.

I've narrowed down the issue and filed a bug internally. I'm closing this bug in favor of: https://github.com/firebase/firebase-functions/issues/27 because the bug is in Cloud Functions rather than our samples.

Was this page helpful?
0 / 5 - 0 ratings