单页面应用程序

Flask 可用于通过将前端框架生成静态文件放置在项目内的子文件夹中来服务单页面应用程序 (SPA)。您还需要创建一个全能端点,将所有请求路由到您的 SPA。

以下示例演示了如何与 API 一起服务 SPA

from flask import Flask, jsonify

app = Flask(__name__, static_folder='app', static_url_path="/app")


@app.route("/heartbeat")
def heartbeat():
    return jsonify({"status": "healthy"})


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return app.send_static_file("index.html")