[英]I get a 404 error on Heroku but it works locally
My app works locally (when running Heroku local in the terminal), with the correct routes.我的应用程序在本地运行(在终端本地运行 Heroku 时),路线正确。 However when I move onto the Heroku server the app is built fine but then just shows a 404 error when I go onto the app.
但是,当我移至 Heroku 服务器时,应用程序构建良好,但当我将 go 移至应用程序时,仅显示 404 错误。 The Heroku.logs return something like this for whichever page I go onto.
Heroku.logs 对于我 go 到的任何页面都会返回类似的内容。
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
I will as little code as I think is needed here to keep it succinct, if there's any more that would be useful to you then let me know and ill put it up also.我将尽可能少的代码来保持它的简洁,如果还有更多对你有用的东西,请告诉我,我也会把它贴出来。 The git commit/push/add are all upto date.
git 提交/推送/添加都是最新的。
Procfile档案
web: cd api && export APP_SETTINGS=config.cfg && gunicorn wsgi:app --log-file -
wsgi.py wsgi.py
from application.__init__ import create_app
app = create_app()
if __name__ == "__main__":
app.run()
init .py初始化.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路线.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应用程序.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;
Finally managed to sort this.终于设法对此进行了排序。 In case anyone else needs help.
以防其他人需要帮助。
There was a few issues with this.这有一些问题。 I first managed to allow navigation on the local version by changing my routes to this:
我首先设法通过将路线更改为允许在本地版本上导航:
@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')
Then the other issue was that I had declared the static folder as one from my laptop as opposed to a relative one.然后另一个问题是我已将 static 文件夹声明为我的笔记本电脑中的文件夹,而不是相对文件夹。 I changed init .py to this which solved the issue:
我将init .py 更改为此解决了问题:
app = Flask(__name__, static_folder=os.path.abspath("../build"), static_url_path='/')
I think that all of this pain could have been avoided by using something like Nginx, but I couldn't quite get my head around it so this will do as a workaround for now!我认为使用 Nginx 之类的东西可以避免所有这些痛苦,但我无法完全理解它,所以现在这将作为一种解决方法!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.