6
votes

I'm new to firebase and trying to make simple Cloud Functions + Express sample from video below to work.
https://www.youtube.com/watch?v=LOeioOKUKI8

When I try to serve my index.js from http://localhost:5000/timestamp, I get following error.

Cannot GET /{my-project-id}/us-central1/app/timestamp

In my terminal I get following output.

⚠ Default "firebase-admin" instance created!
i functions: Finished "app" in ~1s
[hosting] Rewriting /timestamp to http://localhost:5001/{my-project-id}/us-central1/app for local Function app

But if I deploy, It works as expected and it will show my timestamp.

My current code is below.

index.js

var admin = require("firebase-admin");
admin.initializeApp();

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

const app = express();

app.get('/timestamp', (request, response) => {
  response.send(`${Date.now()}`);
});

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

firebase.json

{
  "hosting": {
    "public": "public",
    "rewrites": [{
      "source": "/timestamp",
      "function": "app"
    }],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

If I rewrite part of my index.js to something like,

app.get('/{my-project-id}/us-central1/app/timestamp', (request, response) => {
  response.send(`${Date.now()}`);
});

it will show my timestamp when I access http://localhost:5000/timestamp.
Anyone have any idea why this is happening?

4

4 Answers

6
votes

On my side, i was try to develop a Rest API with firebase cloud functions. I have got same error. If i push my code firebase servers via 'Firebase deploy' It was running what i want. But when i run on my local server via 'firebase serve --only functions, hosting' command there will be always error like Cannot GET and not run. I was try your code and same here. I found a ridiculously simple solution for that and run on my side. Could you try on locally,

 app.get('*/timestamp', (request, response) => {
   response.send(`${Date.now()}`);
 });

Just add * before the your path.

UPDATE :

'firebase-tools' has been updated. If you update your buggy version that 6.9.2 to 6.10.0 the problem has been fixed.

For update latest firebase-tools:

npm i -g firebase-tools
3
votes

For those coming here in recent times, firebase emulators:start is default way to start if you use firebase init to create your firebase functions.

Adding */ is of course one of the way to go forward.

The reason for Cannot /GET /* could be the way firebase exports the api.

After creating the api,

app.get('/app/testapi', (req, res) => {
  res.send({ 'status': 0});
});

when it is exported as exports.app = functions.https.onRequest(app); the actual api becomes app/app/testapi as firebase exports the api on app, (export.app).

You can remove the extra app from here:

app.get('/app/testapi', (req, res) => {
  res.send({ 'status': 0});
});

which then becomes :

app.get('/testapi', (req, res) => {
  res.send({ 'status': 0});
});

This will be available in app/testapi/.

2
votes

For some newcomers, start using firebase emulators:start to test functions locally before deploying as this is a part of new Firebase Emulator Suite. firebase serve is deprecated now

0
votes

I face the same problem as you. And I fix it by revert firebase-tools to 6.8.0 and the problem solve !

npm install --global [email protected]

As for the ref: https://github.com/firebase/firebase-tools/issues/1280