我正在尝试使用Tornado,发现每次更改JavaScript或HTML文件时看起来都没有改变。
我对Node.js很有经验,并且效果很好-如果更改与服务器端无关,那么一切都会自动更新。
如何解决这个问题?当然,我可以每次都停止服务器,但它既费时又乏味。
谢谢。
答案 0 :(得分:0)
在龙卷风应用程序所在的项目文件夹中,您可以使用自动重载模块监听更改。例如
from os import walk
from socket import gethostname
from tornado import autoreload
from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.httpserver import HTTPServer
from tornado.web import RequestHandler
from tornado.options import define, options, parse_command_line
class TestHandler(RequestHandler):
def get(self):
self.render("index.html")
class TestApplication(Application):
def __init__(self, host=None, port=None):
handlers = [
(r"/", TestHandler)
]
settings = {
"autoreload": True,
"compiled_template_cache": True,
"debug": True,
"static_path": "static",
"template_path": "templates"
}
Application.__init__(self, handlers, **settings)
self.host = host
self.port = port
def __call__(self):
if self.port is None:
define("port", default=8001, help="run on the given port", type=int)
else: define("port", default=self.port, help="run on the given port", type=int)
if self.host is None:
define("host", default=gethostname(), help="run on host address", type=str)
else: define("host", default=self.host, help="run on host address", type=str)
parse_command_line()
http_server = HTTPServer(self)
try:
http_server.listen(options.port, address=options.host)
except (KeyboardInterrupt, SystemExit):
print("*!* keyboard interrupt detected...exiting.")
except:
print("*!* error occured...httpserver not listening.")
else:
print("*+* server listening on {host} {port}...".format(host=options.host, port=options.port))
autoreload.start()
for dir, _, files in walk('.'):
[autoreload.watch(dir + '/' + f) for f in files if f.endswith("js") or f.endswith("css") or f.endswith("html")]
IOLoop.current().start()
然后直接调用它,只要您对以指定后缀结尾的文件进行更改,它将自动重新加载服务器。即.js,.html,.css
答案 1 :(得分:0)
问题尚不完全清楚,但是假设您正在从Tornado的StaticFileHandler提供HTML和JS,则需要将debug=True
或static_hash_cache=False
传递给Application
构造函数。