首页 > 解决方案 > Elastic Beanstalk 上的单页应用程序的路由问题

问题描述

使用 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')

标签: pythonamazon-web-servicesflaskamazon-elastic-beanstalksingle-page-application

解决方案


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

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

推荐阅读