【问题标题】:404 page not found when running firebase deploy运行 firebase deploy 时找不到 404 页面
【发布时间】:2020-01-25 12:14:57
【问题描述】:

我使用 react 和 Google 的 firebase 为 auth 和数据库构建了一个令人惊叹的网络应用程序。在我的本地服务器上,一切运行良好。我可以验证某些用户并在成功登录后导航到下一页。当使用 Firebase 托管部署我的网络应用程序时,问题就开始了。

所以,首先安装了firebase-tools,然后运行npm run build,然后我初始化了firebase(我已登录)。然后我跑了firebase deploy。在回答了提示的问题后,给了我一个 URL 到我应该工作的网站。

现在,当我使用应该成功登录的详细信息登录时,我收到以下错误:

404 网页未找到 在此网站上找不到指定的文件。请检查 URL 是否有错误,然后重试。 为什么我会看到这个? 此页面由 Firebase 命令行界面生成。要修改它,请编辑项目配置的公共目录中的 404.html 文件。

要实时查看此错误,这里是the link

成功登录后应该将用户导航到下一页的部分代码如下所示:

import React, { Component } from 'react';
import fire from './Firebase';
import List from './components/List';

class Login extends Component {
  constructor(props) {
    super(props);
    this.login = this.login.bind(this);
    this.handleChange = this.handleChange.bind(this);
    this.state = {
      email: '',
      password: ''
    };
  }

  handleChange(e) {
    this.setState({ [e.target.name]: e.target.value });
  }

  login(e) {
    e.preventDefault();
    fire.auth().signInWithEmailAndPassword(this.state.email, this.state.password).then((u)=>{
        window.location = 'list'; 
    }).catch((error) => {
        console.log(error);
        alert("You don't have priviledges to log in!")
      });
  }

这是 firebase.json 中的代码 sn-p:

{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  },
  "database": {
    "rules": "database.rules.json"
  },
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  }
}

函数的错误:

=== Deploying to 'my-tutor-d4133'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /home/superlelo/Desktop/myTutor/mytutor/functions
> eslint .

sh: 1: eslint: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! functions@ lint: `eslint .`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/superlelo/.npm/_logs/2019-09-26T21_15_49_018Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code1

我不知道如何解决这个问题,有人可以帮忙吗?

谢谢。

【问题讨论】:

  • 我怀疑您在firebase.json 文件中的hosting -> public 条目没有指向您构建的目录。如果您正在使用 React 构建单页应用程序,请仔细检查您是否有一个 rewrites 条目以将所有请求重定向到 index.html
  • @JeremyW 谢谢,我不确定我是否以正确的方式进行双重检查。我该怎么做你说我应该做的,我如何“仔细检查我有一个重写条目来将所有请求重定向到 index.html”
  • Edit 您的帖子并包含您的firebase.json 文件的内容,我将添加一个解释它的答案。
  • 另外,在您的package.json 文件中,您的build 脚本是做什么的?请也将其包含在您的编辑中。你在使用 CreateReactApp 吗?

标签: reactjs firebase authentication


【解决方案1】:

您的firebase.json 文件的hosting->public 条目正确指向build 目录。那挺好的。那是output directory for Create React App。除非您有意更改 CRA 的构建位置,否则 Firebase 应该部署该 build 文件夹中的任何内容。

由于您希望路由发生在客户端 - 由 React 处理 - 您需要后端 (Firebase) 为您的 React 网站提供所有请求的路由。为此,您可以在配置中添加 rewrites 部分:

"hosting": {
    "public": "build",
    "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**"
    ],
    "rewrites": [
        {
            "source": "**",
            "destination": "/index.html"
        }
    ]
}

【讨论】:

  • 所以 - 我必须使用函数添加管理员权限,我遇到了最后添加的错误。我运行了“firebase deploy --only functions”,你知道解决方法吗?
  • 你试过this answer吗?
  • 我已经尝试了所有这些选项,但它们都不起作用。您是否使用足够多的功能来预测可能的原因并解决问题?
  • 没有那个错误。我建议您针对此问题发布一个单独的问题,有人可能有您尚未找到的解决方案。确保在新问题中列出您尝试过的内容 :) 祝你好运!
  • 确实是index.html开头的/帮我解决了这个问题,谢谢
猜你喜欢
  • 2020-12-14
  • 2021-08-15
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 2016-08-17
  • 2016-08-01
  • 2016-08-01
  • 2015-10-22
相关资源
最近更新 更多