Functions-samples: Function returns 404 when accessed from hosting

Created on 31 Mar 2019  路  8Comments  路  Source: firebase/functions-samples

I'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
Console log

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

Most helpful comment

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'

All 8 comments

I think you need to add a slash to the path in your functions code:

Instead of app.get('login',.., try app.get('/login',.., like in this example:

https://github.com/firebase/functions-samples/blob/958a7bc7d99fa241799ead1c68c997848b4e7987/authorized-https-endpoint/functions/index.js#L74

Hey @jhuleatt deployed after changing it to '/login' still facing problem.
I even deployed the same sample app, that too didn't work.

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'

Hey @rolandszpond It worked, thanks for the help :)

hey @sumitkanoje @rolandszpond I'm facing a similar issue, direct http request to the function works great, but when accessing through hosting using the rewrites rule I'm getting a 404, any guess ?

For me it was an issue due to my node.js version. I was using node 12. The npm start always told me, that this was incompatible, Maybe try again w/ node 10 or 8

I also ran into this issue but I forgot to RTFM:

Firebase Hosting supports Cloud Functions in us-central1 only

my function isn't running in us-central1, so it won't work :(

@lukasvan3l Damn, I think I am in the same situation.. Does it mean I won't be able to run a Next JS solution using the Firebase Hosting/Functions in europe-west1 region? :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

motss picture motss  路  4Comments

abhilash1in picture abhilash1in  路  4Comments

inglesuniversal picture inglesuniversal  路  5Comments

IchordeDionysos picture IchordeDionysos  路  3Comments

6vedant picture 6vedant  路  5Comments