使用装饰器配置路由的
上面的 “Hello World” 修改如下:
# tornadoapp.py
import tornado.ioloop
import tornado.web
class Application(tornado.web.Application):
def route(self, pattern):
def _(handler):
handler_pattern = [(pattern, handler)]
self.add_handlers(".*$", handler_pattern)
return handler
return _
app = Application()
@app.route(r"/")
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
if __name__ == "__main__":
app.listen(8888)
tornado.ioloop.IOLoop.instance().start()