question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function returns 404 when accessed from hosting

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

11reactions
rolandszpondcommented, Apr 21, 2019

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’

3reactions
lukasvan3lcommented, Jun 19, 2020

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 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firebase functions return 404 - Stack Overflow
I am trying to set up a custom domain for firebase functions. Custom domain already verified and works fine. My firebase functions run...
Read more >
404 Not Found when trying to run a newly created function
When trying to run a newly created function from the Develop page I'm getting a 404 Not Found error. The function is a...
Read more >
Azure functions http trigger returning 404
This is an http status which indicates that resource which you are trying to access it not available to serve your http request....
Read more >
How to Fix WordPress 404 Not Found Error [8 Easy Solutions]
The 404 error is an HTTP response code that occurs when the server cannot find the file or page requested by the user....
Read more >
How to Fix Error 404 Not Found on Your WordPress Site - Kinsta
Basically, it means that the client (your, or your visitor's, web browser) was able to successfully connect to the host (your website's server), ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found