【问题标题】:flask+react website using App Engine, url not found使用 App Engine 的烧瓶 + 反应网站,找不到网址
【发布时间】:2020-09-17 13:53:40
【问题描述】:

我已部署到 App Engine 的一个简单的 flask+ react 网站在本地运行良好。 当我在本地提供 main.py 并转到 /hello 时,它应该返回“hello world”。但是,当部署到应用引擎并使用提供的 URL 和 /hello 时,它会返回“在此服务器上找不到请求的 URL /hello。”

下面是main.py

from flask import Flask, render_template
app = Flask(__name__, static_folder="./build/static", template_folder="./build")
 @app.route("/")
 def index():
     return render_template("index.html")
@app.route("/hello")
def hello():
    return "Hello World!"
if __name__ == "__main__":
    app.run(host='127.0.0.1', port=8080, debug=True)

这是我的 app.yaml

runtime: python37
entrypoint: gunicorn -b :$PORT main:app

handlers:
- url: /
  static_files: build/index.html
  upload: build/index.html
- url: /
  static_dir: build
- url: /(.*)
  script: auto
project_root/
build/
   static/
   index.html
app.yaml
main.py

输入 [provided-url].com/ 时,它还会在应用引擎上正常返回 index.html

【问题讨论】:

    标签: reactjs google-app-engine flask yaml app.yaml


    【解决方案1】:

    所以在我的代码搞砸了几天之后,问题出在处理程序上。对于runtime:python37,您不应该使用处理程序。不知道为什么,但是当我从 app.yaml 中删除处理程序时,一切正常。

    【讨论】:

    • 如果问题仍然存在或与 Google 云服务相关的任何其他问题,我想将您定向到 Public Issue Tracker。问题跟踪器是 Google 内部使用的一种工具,用于在产品开发过程中跟踪错误和功能请求。更多内容请参见documentation
    【解决方案2】:

    您在 app.yaml 中的 url 处理程序发生短路。第一个 / 匹配,它永远不会到达第二个 / 处理程序。我想你想要这样的东西:

    handlers:
    - url: /static
      static_dir: build/static/
    - url: /(.*)
      script: auto
    

    然后让您的 Flask 处理程序完成所有工作。然后,将所有对静态文件的调用发送到/static/somefile.jpg 等。

    编辑:如果您想让您的生活更轻松,请不要将index.html 作为静态文件提供。从 main.py 提供它,因为你已经设置好了。你所有的 React url 都在 main.py 中处理,并且都单独渲染 index.html。你所有的 React 路由和组件都是从 index.html 内部调用的。您的 API 调用也会在 main.py 中处理。

    @app.route("/")
    def index():
        template_context = {
            'msg'      : "you are on my home page",
        }
        return render_template("index.html", **template_context)
    
    @app.route("/profile")
    def profile():
        template_context = {
            'msg'      : "this is your user profile",
        }
        return render_template("index.html", **template_context)
    
    @app.route("/hello")
    def hello():
        return "Hello World!"
    

    编辑#2:如果您坚持将 index.html 作为静态文件提供,您可以像这样设置您的 app.yaml:

    handlers:
    - url: /
      static_files: build/index.html
      upload: build/index.html
    - url: /static
      static_dir: build/static/
    - url: /(.*)
      script: auto
    

    你会从 main.py 中删除 @app.route("/") 处理程序,因为它永远不会到达那里。

    【讨论】:

    • 这是一个反应应用程序。我希望它为 index.html 服务并让反应来处理它的路线。但也能够响应 /hello 之类的 API 调用。所以前两个处理程序用于在用户访问主页时发送 index.html。
    • 但是 / 并不匹配所有内容。我的 app.yaml 来自谷歌文档。您的解决方案返回服务器错误 500。The server encountered an error and could not complete your request. 当我在本地运行我的代码时,它运行良好。
    • 您说的是 app.yaml 或 main.py 中的 url 处理程序吗?链接到您从中复制的文档会很有帮助。
    猜你喜欢
    • 2014-09-22
    • 1970-01-01
    • 2016-04-02
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多