Python 我在Heroku上得到一个404错误,但它在本地工作
我的应用程序在本地工作(在终端中运行Heroku local时),具有正确的路由。然而,当我进入Heroku服务器时,应用程序构建良好,但当我进入应用程序时,只显示404错误。无论我进入哪个页面,Heroku.log都会返回类似的内容Python 我在Heroku上得到一个404错误,但它在本地工作,python,reactjs,flask,heroku,Python,Reactjs,Flask,Heroku,我的应用程序在本地工作(在终端中运行Heroku local时),具有正确的路由。然而,当我进入Heroku服务器时,应用程序构建良好,但当我进入应用程序时,只显示404错误。无论我进入哪个页面,Heroku.log都会返回类似的内容 2020-07-16T17:24:44.233685+00:00 heroku[router]: at=info method=GET path="/" host=advancedmatchedbetting.herokuapp.com req
2020-07-16T17:24:44.233685+00:00 heroku[router]: at=info method=GET path="/" host=advancedmatchedbetting.herokuapp.com request_id=315ee036-cb58-4493-9b46-c2a25337070e fwd="176.250.1.224" dyno=web.1 connect=1ms service=3ms status=404 bytes=400 protocol=https
我将尽可能少的代码,因为我认为需要在这里保持它简洁,如果有任何更多的,将是有益的,你让我知道,我会把它也。git提交/推送/添加都是最新的
程序文件
web: cd api && export APP_SETTINGS=config.cfg && gunicorn wsgi:app --log-file -
wsgi.py
from application.__init__ import create_app
app = create_app()
if __name__ == "__main__":
app.run()
init.py
db = SQLAlchemy()
def create_app():
app = Flask(__name__, static_folder='/Users/ianholdroyd/Documents/GitHub/advancedmatchedbetting_react/build', static_url_path='/')
db.init_app(app)
app.config.from_envvar('APP_SETTINGS')
with app.app_context():
from . import routes
db.create_all()
return app
routes.py
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/api/signup', methods=['POST'])
def signup():
data = request.get_json()
new_user = newsletter(
first_name=data['firstName'],
last_name=data['lastName'],
email= data['email'],
created=dt.now()
)
db.session.add(new_user) # Adds new User record to database
db.session.commit()
return("")
config.cfg
SECRET_KEY = ****
SQLALCHEMY_TRACK_MODIFICATIONS = False
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///static/site.db'
app.js
function App() {
return (
<Router>
<Switch>
<Route exact path="/" component={homepage} />
<Route exact path = "/404" component={NotFoundPage} />
<Route exact path="/calculator/" component={Calculator} />
<Route exact path="/blogpost/:id" component={blogpostpage} />
<Route exact path="/signup" component={signuppage} />
<Redirect to="/404"/>
</Switch>
</Router>
);
}
export default App;
函数应用程序(){
返回(
);
}
导出默认应用程序;
终于对其进行了排序。以防其他人需要帮助
这方面有一些问题。我首先通过将我的路线更改为以下方式,允许在本地版本上导航:
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
return app.send_static_file('index.html')
@app.errorhandler(404)
def not_found(e):
return app.send_static_file('index.html')
我认为所有这些痛苦都可以通过使用像Nginx这样的东西来避免,但是我不能完全理解它,所以现在这将作为一个解决办法 快速更新可能会有所帮助:例如,如果我尝试导航到非主页的页面,我会在本地遇到相同的404错误,但是,如果我首先打开主页,然后从那里导航到计算器页面,通常会出现此页面。我认为这可能与进入react应用程序需要应用程序路径“/”有关,但是我不确定为什么我甚至无法从Heroku页面访问此应用程序?
app = Flask(__name__, static_folder=os.path.abspath("../build"), static_url_path='/')