如何使用多个静态文件夹处理 Tornado 中的静态文件处理程序?

How to handle static files handler in Tornado with several static folders?

我目前的路由表如下:

routing_table = [
        ("/api/ping", PingHandler),
        ("/css/(.*)", StaticFileHandler, {
             "path": "my-website-path/css"
        }),
        ("/js/(.*)", StaticFileHandler, {
             "path": "my-website-path/js"
        }),
        ("/fonts/(.*)", StaticFileHandler, {
             "path": "my-website-path/fonts"
        })

我只想使用一个正则表达式来处理我的静态文件。 像下面这样的东西?

routing_table = [
        ("/api/ping", PingHandler),
        ("/(css|js|fonts)/(.*)", StaticFileHandler, {
             "path": "my-website-path/"
        })

我该怎么做? 提前谢谢你。

A RequestHandler 将所有匹配项作为位置参数传递给 http-verb 函数。由于 StaticFileHandler 扩展了它并且您有 2 个捕获的组,因此您的代码将无法按预期工作。因此需要逐步更改正则表达式:

  1. 匹配整个路径:/(.*)
  2. 第一部分应该是字体,js或者css:((jss|css|fonts)/.*
  3. 内部组不应该被捕获-利用?:((?:jss|css|fonts)/.*

密码

routing_table = [
        ("/api/ping", PingHandler),
        ("/((?:css|js|fonts)/.*)", StaticFileHandler, {
             "path": "my-website-path"
        }

请记住,StaitcFileHandler(如@cricket_007 所述)...

This handler is intended primarily for use in development and light-duty file serving; for heavy traffic it will be more efficient to use a dedicated static file server (such as nginx or Apache). We support the HTTP Accept-Ranges mechanism to return partial content (because some browsers require this functionality to be present to seek in HTML5 audio or video).