使用 Tornado 运行 HTTP 服务器并使用 Ember 生成的内容时,无法刷新页面



注意:我是龙卷风的新手。

要重现我的问题,我有以下 Python 文件:

import tornado.httpserver
import tornado.ioloop
import tornado.web
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.render("dist/index.html")
def start_server():
app = tornado.web.Application([
(r'/', IndexHandler),
(r'/assets/(.*)', tornado.web.StaticFileHandler, {"path": "dist/assets"}),
])
ioLoopInstance = tornado.ioloop.IOLoop.instance()
http_server = tornado.httpserver.HTTPServer(app, io_loop=ioLoopInstance)
http_server.listen(34567)
ioLoopInstance.start()
if __name__ == '__main__':
start_server()

然后,我有一个 Ember 项目,它有两个路由(/route1 和/route2(,每个路由上都有一个按钮,它只是过渡到另一个路由,还有一个应用程序路由在 beforeModel 中转换到 route1。我将 Ember 生成的 dist 目录复制到包含上面 Python 文件的目录中。当我运行 Python 文件并导航到 localhost:34567 时,这会自动转换为 localhost:34567/route1,然后我可以按下按钮在页面之间导航。但是,当我刷新页面或在地址栏中输入 localhost:34567/route1 时,我得到一个"404:未找到"页面。我的龙卷风设置中缺少什么?

谢谢

Ember(作为大多数竞争对手(拥有自己的基于客户端的路由器,它使用pushState(或/和哈希路由(。除非你这样做,否则 Ember 不会向后端发出任何请求。

解决方案很简单,在龙卷风中,IndexHandler用于您期望的所有路径。常见的方法是处理所有路径,但资产除外。例:

# note 1: '.*' match all
# note 2: Tornado router matches paths in the order,
#         so match all should be the last
app = tornado.web.Application([
(r'/assets/(.*)', tornado.web.StaticFileHandler, {"path": "dist/assets"}),
(r'/.*', IndexHandler),
])

最新更新