Tornado 静态文件路由
Tornado static file routing
我正在尝试准备默认错误页面。在 error.html
文件中我使用:
<link href="css/bootstrap.min.css" rel="stylesheet">
在龙卷风中 Application
我使用以下路由指令:
(r'/css/(.*)', web.StaticFileHandler, {'path': 'assets/css'}
假设我输入 http://localhost:8888/api
url。一切正常,css 文件加载正确,错误页面呈现正常。但是,当我键入 http://localhost:8888/api/foo
时,找不到 css 文件。
在第一种情况下,处理程序正确处理了 css 请求 http://localhost:8888/css/bootstrap.min.css
。在第二种方法中,对 css 的请求被转换为未处理的 http://localhost:8888/api/css/bootstrap.min.css
。
我希望在两种情况下都能找到的资源正确显示错误页面。我可以使用:
(r'.*/css/(.*)', web.StaticFileHandler, {'path': 'assets/css'}
然而这次我可以在浏览器中输入 http://localhost:8888/api/asdasdsadsad/css/bootstrap.min.css
url 并且 css 文件被显示,而我认为应该显示错误页面。我怎样才能摆脱这个问题?
因为你使用了相对路径。有两种常见的方法可以解决此问题:
使用绝对 URL。在任何资源之前添加 /
(斜杠),因此不要使用 <link href="css/bootstrap.min.css" rel="stylesheet">
,而是使用 <link href="/css/bootstrap.min.css" rel="stylesheet">
将 <base>
添加到 <head>
部分的页面
Base ... specifies the base URL to use for all relative URLs contained within a document.
<base href="http://www.example.com/">
并将处理程序保留为
(r'/css/(.*)', web.StaticFileHandler, {'path': 'assets/css'})
后者之所以有效,是因为它的正则表达式非常广泛,正如您注意到它处理的请求应该是 404
。最好的做法是让路线尽可能具体。
我正在尝试准备默认错误页面。在 error.html
文件中我使用:
<link href="css/bootstrap.min.css" rel="stylesheet">
在龙卷风中 Application
我使用以下路由指令:
(r'/css/(.*)', web.StaticFileHandler, {'path': 'assets/css'}
假设我输入 http://localhost:8888/api
url。一切正常,css 文件加载正确,错误页面呈现正常。但是,当我键入 http://localhost:8888/api/foo
时,找不到 css 文件。
在第一种情况下,处理程序正确处理了 css 请求 http://localhost:8888/css/bootstrap.min.css
。在第二种方法中,对 css 的请求被转换为未处理的 http://localhost:8888/api/css/bootstrap.min.css
。
我希望在两种情况下都能找到的资源正确显示错误页面。我可以使用:
(r'.*/css/(.*)', web.StaticFileHandler, {'path': 'assets/css'}
然而这次我可以在浏览器中输入 http://localhost:8888/api/asdasdsadsad/css/bootstrap.min.css
url 并且 css 文件被显示,而我认为应该显示错误页面。我怎样才能摆脱这个问题?
因为你使用了相对路径。有两种常见的方法可以解决此问题:
使用绝对 URL。在任何资源之前添加
/
(斜杠),因此不要使用<link href="css/bootstrap.min.css" rel="stylesheet">
,而是使用<link href="/css/bootstrap.min.css" rel="stylesheet">
将
<base>
添加到<head>
部分的页面Base ... specifies the base URL to use for all relative URLs contained within a document.
<base href="http://www.example.com/">
并将处理程序保留为
(r'/css/(.*)', web.StaticFileHandler, {'path': 'assets/css'})
后者之所以有效,是因为它的正则表达式非常广泛,正如您注意到它处理的请求应该是 404
。最好的做法是让路线尽可能具体。