【问题标题】:Route bottle server two subfolders?路由瓶服务器的两个子文件夹?
【发布时间】:2014-03-27 13:23:59
【问题描述】:

两个子文件夹位于同一个名为 frontend 的文件夹下。

一个是app,另一个是dist。

在我的 main.py 中,我的路由如下

@bottle.route('/') 
def server_static(filename="index.html"):
  return static_file(filename, root='./frontend/dist/')

@bottle.route('/<filepath:path>')
def server_static(filepath):
  return static_file(file path, root='./frontend/dist/')

现在当用户访问像 www.example.com/ 这样的主站点 URL 时,他们可以成功地从文件夹 dist 加载所有内容。

但我希望为文件夹应用程序添加特定路由。这样当用户访问 www.example.com/dev/ 时,文件夹 app 中的所有内容都会被加载。

我试过了

@bottle.route('/dev') 
def server_static(filename="index.html"):
  return static_file(filename, root='./frontend/app/')

@bottle.route('/dev/<filepath:path>')
def server_static(filepath):
  return static_file(file path, root='./frontend/app/')

但这根本行不通。我认为这是由于我使用了文件路径。

任何人都可以建议在这种情况下如何路由?

【问题讨论】:

    标签: python routing bottle


    【解决方案1】:

    当两个路由匹配时,选择第一个声明的路由。您需要先声明最具体的路线:

    @bottle.route('/') 
    ...
    
    @bottle.route('/dev') 
    ...
    @bottle.route('/dev/<filepath:path>')
    ...
    
    # because this matches the previous two routes, it must come after them
    @bottle.route('/<filepath:path>')
    ...
    

    【讨论】:

    • 奇怪的是,我仍然收到如下错误“INFO 2014-03-27 13:59:21,853 server.py:593] 默认:“GET /dev HTTP/1.1”304 -”即使我遵循序列。有什么建议吗?
    • 304 没问题。这意味着您的浏览器已经有静态文件的副本
    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 2010-09-10
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    相关资源
    最近更新 更多