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?
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