【问题标题】:I get a 404 error on Heroku but it works locally我在 Heroku 上收到 404 错误,但它在本地工作
【发布时间】:2020-11-06 11:05:44
【问题描述】:

我的应用程序在本地运行(在终端中本地运行 Heroku 时),路线正确。但是,当我移动到 Heroku 服务器时,应用程序构建得很好,但是当我进入应用程序时只显示 404 错误。 Heroku.logs 对于我进入的任何页面都会返回类似的内容。

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 commit/push/add 都是最新的。

过程文件

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()

初始化.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;

【问题讨论】:

  • 快速更新可能会有所帮助:如果我尝试导航到不是主页的页面,例如0.0.0.0:5000/calculator,我会在本地收到相同的 404 错误,但是如果我这样做,此页面会正常出现首先打开主页,然后从那里导航到计算器页面。
  • 我认为这可能与进入反应应用程序需要应用程序路由'/'有关,但是我不确定为什么我什至无法访问此应用程序从 Heroku 页面?

标签: python reactjs flask heroku


【解决方案1】:

终于设法对此进行排序。以防其他人需要帮助。

这有一些问题。我首先设法通过更改我的路线来允许在本地版本上导航:

@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')

然后另一个问题是我已将静态文件夹声明为来自笔记本电脑的文件夹,而不是相对文件夹。我将 init.py 更改为此解决了问题:

app = Flask(__name__, static_folder=os.path.abspath("../build"), static_url_path='/')

我认为使用 Nginx 之类的东西可以避免所有这些痛苦,但我无法完全理解它,所以现在这将作为一种解决方法!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-06
    • 2011-08-28
    • 1970-01-01
    • 2022-01-17
    • 2019-10-19
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    相关资源
    最近更新 更多