Function returns 404 when accessed from hosting
See original GitHub issueI’m using sample code to serve dynamic content on firebase I was able to deploy hosting & function both as suggested in sample code but, when I access function url from firebaseapp url it fails, whereas when I access it directly from cloudfunctions url it works
How to reproduce these conditions
"hosting": {
"public": "dist",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/**",
"function": "api"
},
{
"source": "**",
"destination": "index.html"
}
]
},
"database": {
"rules": "database.rules.json"
}
}
Failing Function code used (including require/import commands at the top)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({origin: true}));
app.get('login', (req, res) => {
return res
.type('application/manifest+json')
.status(200)
.send(JSON.stringify({
message: 'OK',
}, null, 2));
});
exports.api = functions.https.onRequest(app);
Steps to set up and reproduce
GET xxxx.firebaseapp.com/api/login => Can not GET api/login | Status 404 GET us-central1-xxxx.cloudfunctions.net/api/login => OK | Status 200
Debug output
Errors in the console log shows that hosting url is proerly forwarded to function but then function return 404
Screenshots
Expected behavior
GET xxxx.firebaseapp.com/api/login => OK | Status 200
Actual behavior
GET xxxx.firebaseapp.com/api/login => Can not GET api/login | Status 404
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top GitHub Comments
Hey @sumitkanoje, I was having the same problem and after some testing, it looks like express endpoints have to include ‘/api’ in the urls, which in the examples, they don’t. So change your ‘/login’ to include ‘/api/login’
I also ran into this issue but I forgot to RTFM:
my function isn’t running in us-central1, so it won’t work 😦