Python 烧瓶和反应路线

Python 烧瓶和反应路线,python,reactjs,flask,url-routing,Python,Reactjs,Flask,Url Routing,我正在用React构建Flask应用程序,但最终遇到了路由问题 后端负责作为API,因此某些路由看起来像: @app.route('/api/v1/do-something/', methods=["GET"]) def do_something(): return something() 以及通向反应器的主要路线: @app.route('/') def index(): return render_template('index.html') 我在React应用程序中使用

我正在用React构建Flask应用程序,但最终遇到了路由问题

后端负责作为API,因此某些路由看起来像:

@app.route('/api/v1/do-something/', methods=["GET"])
def do_something():
    return something()
以及通向反应器的主要路线:

@app.route('/')
def index():
    return render_template('index.html')
我在React应用程序中使用,一切正常,React路由器将我带到
/something
并获得渲染视图,但当我刷新
/something
上的页面时,Flask应用程序将处理此调用,我得到
未找到
错误

最好的解决方案是什么?我正在考虑将所有不调用
/api/v1/..
的调用重定向到
/
这并不理想,因为我将返回我的应用程序主页,而不是呈现的React视图。

我们用于此

from flask import Flask
app = Flask(__name__)

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return 'You want path: %s' % path

if __name__ == '__main__':
    app.run()
从烧瓶导入烧瓶
app=烧瓶(名称)
@app.route('/',默认值={'path':''})
@应用程序路径(“/”)
def catch_all(路径):
返回“您想要的路径:%s”%path
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run()

您还可以多走一步,重新使用
Flask路由
系统,将
路径
匹配到与客户端相同的路由,以便您可以将客户端需要的数据作为JSON嵌入HTML响应中。

可能是之前答案的扩展。这为我解决了问题:

from flask import send_from_directory

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
     path_dir = os.path.abspath("../build") #path react build
     if path != "" and os.path.exists(os.path.join(path_dir, path)):
         return send_from_directory(os.path.join(path_dir), path)
     else:
         return send_from_directory(os.path.join(path_dir),'index.html')
从flask导入从\u目录发送\u
@app.route('/',默认值={'path':''})
@应用程序路径(“/”)
def serve(路径):
path_dir=os.path.abspath(“../build”)#路径反应生成
如果路径!=“”和os.path.exists(os.path.join(path\u dir,path)):
从目录返回发送目录(os.path.join(path\u dir),path)
其他:
从目录返回发送目录(os.path.join(path\u dir),'index.html')

出于某种原因,catch-all URL对我不起作用。我发现使用flask 404处理程序会产生完全相同的结果。它会看到url并将其传递到路由器处理它的位置

@app.errorhandler(404)   
def not_found(e):   
  return app.send_static_file('index.html')

只需告知句柄错误404和render_模板对我来说非常适合

@app.errorhandler(404)
def not_found(e):
    return render_template("index.html")

如果您不希望后端处理URL,那么最快、最简单的方法就是不要使用HTML5历史api。停用它将导致类似于
/#/something
的hashbang URL,重新加载它们将始终触发后端的
/
视图。否则,您需要在后端处理请求或将它们重定向到react应用程序,如果请求的url不是
/
,则react应用程序需要分析请求并触发位置更改。这似乎是某种解决方案,谢谢!我认为这不是一个好的解决办法。现在是2015年,你真的应该使用历史API。@DanAbramov确切地说,但是如何使用呢?还有一种方法——你可以简单地在.htaccess文件中编写一个重写语句,这样每次特定的
#某个
通过react路由器时。如果URL基于某种模式,那么这可能是一个有效的解决方案,如果不是,那么它会浪费时间,当我从前端进行ajax请求调用时,您将如何区分后端api路由和反应路由?@shangyeshen在捕获所有api路由之前注册api路由,以便它们具有优先级。是否有一个完整的例子?这里的代码没有像上面的例子那样进行任何索引渲染,我无法以一种有效的方式将两者结合起来。找到了。我无法让
@app.route(“/”)
匹配任何内容,直到我找到记录不完整的烧瓶参数。设置
static\u url\u path=”“
不正确。我最终得到了
静态url\u path=“/public”,静态文件夹=“../public”
。然后我返回一个静态:
returnapp.send\u static\u文件(“index.html”)
,带有硬编码的
“/public/…”
URL。另外,请看:(不是我)。这是我使用了一段时间的解决方案,直到我需要使用
React.lazy
将包分解为多个部分
React.lazy
希望从
path
而不是flask使用的静态目录获取捆绑包部分。有什么想法吗?你不能用一个普通的路径目录来替换空的os.path.join(路径目录)吗?这很好用!谢谢分享!