棘手且非常丑陋的解决方案:您可以只使用self.write()
而不是self.render()
打印文件的内容。如果它是一个 HTML 页面,那么将会有更多 .css、.js 文件和图像的 GET 请求,因此您必须有第二个处理程序才能将它们全部返回。AngularJS 应用程序示例来自:http ://architects.dzone.com/articles/angularjs-get-first-impression
项目树:
$ tree
.
├── angular_app
│ ├── css
│ │ ├── app.css
│ │ └── bootstrap.css
│ ├── img
│ │ └── ajax-loader.gif
│ ├── index.html
│ └── js
│ ├── app.js
│ ├── contollers
│ │ └── CurrencyConvertCtrl.js
│ ├── db.js
│ ├── models
│ │ └── Currency.js
│ ├── _references.js
│ └── vendor
│ ├── angular.js
│ ├── bootstrap.js
│ ├── highcharts.js
│ └── jquery-1.9.1.js
├── test.py
└── test.py~
龙卷风代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import logging
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
import os
angular_app_path=os.path.join(os.path.dirname(__file__), "angular_app")
class IndexHandler(tornado.web.RequestHandler):
def get(self):
with open(angular_app_path + "/index.html", 'r') as file:
self.write(file.read())
class StaticHandler(tornado.web.RequestHandler):
def get(self):
self.set_header('Content-Type', '') # I have to set this header
with open(angular_app_path + self.request.uri, 'r') as file:
self.write(file.read())
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[(r'/', IndexHandler), (r'/js.*', StaticHandler), (r'/cs.*', StaticHandler), (r'/img.*', StaticHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()