我无法在 Flask 的服务器中加载所有 React 构建文件。当应用程序尝试检索静态文件夹中的其他文件时,我不断收到 HTTP 错误 404。这是我运行 npm run build 后的树结构。我已经将此文件放在与我在 Flask 服务器中的其他文件相同的目录中。我正在尝试将flask+ react 应用程序部署到heroku 仅供参考。
<块引用>routes.py
import datetime
import time
from database import modules
import base64
import schedule
from dotenv import load_dotenv
from pathlib import Path
import os
load_dotenv()
env_path = Path('.')/'.env'
load_dotenv(dotenv_path=env_path)
PRIVATE_KEY = os.getenv("PRIVATE_KEY")
VAPID_PUBLIC_KEY = "BJC37iFk2x1EjU-lVHrivl3QsFe2AiI43rBuXcT-3HX7lElLL4k3xNlmxr7k7tjPDiuzoFcZ8zB4PJB_7opIyW0"
VAPID_PRIVATE_KEY = PRIVATE_KEY
@app.route("/", defaults={'path':''})
def home(path):
return send_from_directory(app.static_folder,'index.html')
<块引用>
init.py
from flask import Flask, request
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from sqlalchemy import *
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from dotenv import load_dotenv
from pathlib import Path
import os
app = Flask(__name__, static_url_path='', static_folder='../static')
bcrypt = Bcrypt(app)
CORS(app)
load_dotenv()
env_path = Path('.')/'.env'
load_dotenv(dotenv_path=env_path)
SECRET_KEY = os.getenv("SECRET_KEY")
app.config['SECRET_KEY'] = SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
migrate=Migrate(app,db) #Initializing migrate.
manager = Manager(app)
manager.add_command('db',MigrateCommand)
@app.before_first_request
def create_tables():
db.create_all()