Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 捕获flask应用程序中的所有路径_Javascript_Python_Html_Angularjs_Flask - Fatal编程技术网

Javascript 捕获flask应用程序中的所有路径

Javascript 捕获flask应用程序中的所有路径,javascript,python,html,angularjs,flask,Javascript,Python,Html,Angularjs,Flask,我在heroku上的Angular+Flask中编写应用程序。我一直在寻找使用HTML5模式为oauth使用myapp.co/register。HTML5模式的问题是,如果有人刷新页面或单击非根目录的链接,服务器需要重写url。我试着用这个片段来捕捉它们,但并没有成功 @app.route("/", defaults={"path": ""}) @app.route("/<path:path>") def index(path): print path return make

我在heroku上的Angular+Flask中编写应用程序。我一直在寻找使用HTML5模式为oauth使用
myapp.co/register
。HTML5模式的问题是,如果有人刷新页面或单击非根目录的链接,服务器需要重写url。我试着用这个片段来捕捉它们,但并没有成功

@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def index(path):
  print path
  return make_response(open(app.static_folder + "index.html").read())

应用程序的根目录是servers index.html和/api/v1/,即RESTful-api

如果您没有更高优先级的路由,那么您的代码将捕获所有URL。但是看起来它应该返回
500
错误(因为我希望您没有

如果此路线有错误,请使用:

@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def index(path)
    return send_from_directory(app.static_folder, "index.html")
@app.route(“/”,默认值={“路径”:“})
@附件路线(“/”)
def索引(路径)
从目录(app.static\u文件夹,“index.html”)返回send\u

您需要使用比您现有的更多的东西:

@app.route("/", defaults={"path": ""})
@app.route("/<string:path>") <--- this is missing
@app.route("/<path:path>")
def index(path):
  print path
  return make_response(open(app.static_folder + "index.html").read())
@app.route(“/”,默认值={“路径”:“})

@app.route(“/”)谢谢。“不安全”是指url映射到文件路径?从目录发送有什么好处?是的,有人可以尝试使用send
。/
~/
发送到路径(可能它不适用于URL,但可能您可以通过任何其他方式获得它)
send_from_directory
(请参阅)足够聪明,可以避免使用
safe_join
的目录的相对路径,并且有许多
send_file
选项。例如,flask
static
使用的路由
send\u from\u directory
。这对开发有好处,但对生产来说,最好使用nginx处理这些文件,或者如果您愿意,用CDN URL替换它。对于使用Flask和AngularJS的任何人,如果您希望您的应用程序在angular上显示当前路线,而不是在刷新页面时吐出404,请使用@app.route(“/”)我不得不从烧瓶初始化中删除“static\u url\u path”以使其工作。我想知道为什么官方文档中缺少这个。见:
@app.route("/", defaults={"path": ""})
@app.route("/<string:path>") <--- this is missing
@app.route("/<path:path>")
def index(path):
  print path
  return make_response(open(app.static_folder + "index.html").read())
@app.route("/<string:path>")