【问题标题】:Routing issue on Single Page Application on Elastic BeanstalkElastic Beanstalk 上的单页应用程序的路由问题
【发布时间】:2020-07-19 19:48:51
【问题描述】:

使用 Flask 作为 Web 服务器,使用 Angular 作为我在 /static 目录中的 SPA。我在我的 Flask 应用程序上实现了一个包罗万象的端点,以尝试解决用户在刷新页面时得到404 的问题,但这并没有解决问题。有谁知道如何解决这个问题?

包罗万象的样子:

@application.route('/', defaults={'path': ''}) 
@application.route('/static/<path:path>') 
def main(path): 
    return render_template('index.html')

如果抛出 404,我还实现了重定向,但这也无济于事:

@application.errorhandler(Exception)
@cross_origin()
def main_404(*args, **kwargs):
    logger.info("Routing passed to web application...") 
    return render_template('index.html')

【问题讨论】:

  • 如果你路由到/static/,你还没有路由到/static/&lt;path:path&gt;。 ;这可能是 catch all 处理程序的问题。
  • 这个问题似乎是由于 /static 目录。如果我输入 elasticbeanstalkurl.com/foobar/foo 我的 404 重定向效果很好。但是当用户在页面上并且他们点击刷新时,url 是 elasticbeanstalkurl.com/static/foo 他们得到一个 404。这是因为 beanstalk 试图在 /static 目录中找到一个名为 /foo 的文件(有点像/static/index.html 如何将它们带到应用程序中)。知道如何阻止这种情况吗?

标签: python amazon-web-services flask amazon-elastic-beanstalk single-page-application


【解决方案1】:

如果您想捕获错误,请尝试使用错误处理程序:

@application.errorhandler(404)
def page_not_found(error):
    app.logger.error('Page not found: %s', (request.path))
    return render_template('404.html'), 404

【讨论】:

  • 我希望能起作用的正是那个errorHandler,除了“return render_template('index.html')”,但没有运气——奇怪的是用户仍然得到一个404。我什至试着只返回一个用于测试函数的基本字符串,但仍返回 404。
  • 我更新了问题中的代码 sn-p 以反映这一点。
  • 应用程序是否仍然调用page_not_found 错误或应用程序忽略错误处理程序?
猜你喜欢
  • 2018-11-02
  • 2015-06-12
  • 2016-05-29
  • 2019-04-20
  • 2023-03-09
  • 2012-09-22
  • 2016-10-29
  • 2018-11-09
  • 1970-01-01
相关资源
最近更新 更多