单页应用

Flask 可以用为单页应用( SPA )提供服务,实现方式是把前端框架生成的静态文件放在项目的子文件夹中。你还需要创建一个全包端点把所有请求指向你的 SPA 。

下面的演示如何用一个 API 为 SPA 提供服务:

  1. from flask import Flask, jsonify
  2. app = Flask(__name__, static_folder='app')
  3. @app.route("/heartbeat")
  4. def heartbeat():
  5. return jsonify({"status": "healthy"})
  6. @app.route('/', defaults={'path': ''})
  7. @app.route('/<path:path>')
  8. def catch_all(path):
  9. return app.send_static_file("index.html")