Firebase云功能+托管 - 找不到404

时间:2018-02-28 02:19:05

标签: firebase express google-cloud-functions firebase-hosting incognito-mode

我在Firebase云端功能和托管之上构建我的网络应用程序。我的客户端是使用create-react-app构建的,我有一个运行在云功能中的小型快速服务器,用于构建应用程序。

在普通的浏览器窗口中一切正常但是,我遇到了一个问题,如果我去任何不是' /'在隐身模式或移动设备中,我收到了404和#34;未找到"

即:点击" https://144blocks.com/week"直接将在正常的浏览器窗口中正常加载,但在隐身和移动设备中引发以下错误。

enter image description here

我为该网站提供服务的firebase功能是:

功能/ index.js

const functions = require('firebase-functions');
const express = require('express');
const fs = require('fs');
const path = require('path');

const app = express();

app.get('/*', (req, res) => {
    const fullPath = path.normalize(__dirname + '/../react-ui/build/index.html');

    res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
    res.sendFile(fullPath);
});

exports.app = functions.https.onRequest(app);

在我的客户端我的firebase.json文件:

firebase.json

{
  "hosting": {
    "public": "react-ui/build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "app"
      }
    ]
  }
}

我不确定为什么除了' /'之外我无法直接击中任何路线。在隐身窗口或移动设备中。理想情况下,我希望我的所有路由都流过我的快速服务器上的app.get(/*,并让我的客户端处理路由。

3 个答案:

答案 0 :(得分:1)

Firebase托管目前存在问题,这似乎影响了许多网站,包括我自己的两个网站。您可以通过此link保持最新状态更新。

答案 1 :(得分:0)

我仍然不能100%确定为什么这样做但是我通过以下方式解决了这个问题: 1.在我的functions/index.js文件中,更新fullPath,因为我认为__dirname + '/../react-ui/build/index.html'尚未解决。

我的姓名functions/index.js档案:

const functions = require('firebase-functions');
const express = require('express');
const path = require('path');

const app = express();

app.get('/*', (req, res) => {
    const fullPath = path.normalize(__dirname + '/build/index.html');

    res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
    res.sendFile(fullPath);
});

exports.app = functions.https.onRequest(app);
  1. 在我的构建过程中,我将构建目录移动到functions文件夹中以使用这些函数部署到Firebase,因此当我准备好部署时,我的项目目录就像构建目录一样functionsreact-ui完全相同: enter image description here
  2. 如果有人更深入地了解为什么这种情况确实有效,我将非常感谢您提供更多信息。

    谢谢!

答案 2 :(得分:0)

尝试运行firebase login。我遇到了这个确切的问题,这是因为我的登录会话已过期。

或者,如果您在使用标准登录时遇到问题,请尝试firebase login --no-localhost

如果您使用的是ci / cd,请使用firebase login:ci --no-localhost,将ci令牌保存到FIREBASE_TOKEN下的环境中,并使用firebase deploy --only functions --token $FIREBASE_TOKEN进行部署。

相关问题