Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot GET" Error on Firebase Cloud Functions + Express sample

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?

like image 474
ysjn Avatar asked May 14 '19 10:05

ysjn


People also ask

Does Firebase Functions use Express?

All requests to HTTPS triggered Cloud Functions are automatically parsed by Cloud Functions with an Express Body Parser before the request reaches your code in the function. So even if you define your parser and middleware through Express, it's still already automatically parsed.

Does Cloud Functions use Express?

You can use web frameworks, like Express. js, in Cloud Functions to serve your app's dynamic content and write complex web apps more easily. Firebase Hosting also supports HTTPS requests to Cloud Run containers.

Why is Firebase Functions not free?

Cloud Functions for Firebase does have a free tier for usage up to a certain point, as outlined on our pricing page. However, because Cloud Functions uses some aspects of Google Cloud's paid infrastructure, we need to enable billing to use Cloud Functions.


2 Answers

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
like image 128
Twinsens Avatar answered Sep 20 '22 07:09

Twinsens


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/.

like image 24
Devayan Sarkar Avatar answered Sep 20 '22 07:09

Devayan Sarkar