@app.route("/book") class BooksResource(object): def get(self, req, resp): resp.text = "Books Page" def handler1(req, resp): resp.text = "handler1" def handler2(req, resp): resp.text = "handler2" app.add_route("/handler1", handler1) app.add_route("/handler2", handler2) @app.route("/index") def index(req, resp): template = app.template("index.html", context={ "name": "二两", "title": "ToyWebF" }) # resp.body需要bytes,template方法返回的是unicode string,所以需要编码 resp.body = template.encode() def custom_exception_handler(request, response, exception_cls):
# exception handler def custom_exception_handler(request, response, exception_cls): response.txt = str(exception_cls) app.add_exception_handler(custom_exception_handler) # Django-like handlers def handler(req, resp): resp.text = "sample" app.add_route("/sample", handler) # class-based handlers @app.route('/book') class BookResource: def get(self, req, resp): resp.text = "Books Page." def post(self, req, resp): resp.text = "Endpoint to create a book." @app.route('/book/{id:d}/update') class PutBook: def put(self, req, resp, id):
def sum(request, response, x, y): total = int(x) + int(y) response.text = f'{x} + {y} = {total}' @app.route('/book') class BooksResource: def get(self, request, response): response.text = 'Books Page' def post(self, request, response): response.text = 'Endpoint to create a book' def handler01(request, response): response.text = 'YOLO 01' def handler02(request, response): response.text = 'YOLO 02' app.add_route('/yolo_01', handler01) app.add_route('/yolo_02', handler02) @app.route('/home') def home(request, response): context = {'title': 'Awesome Framework', 'name': 'Bumbo'} response.body = app.template('home.html', context=context).encode()
<style> body{ text-align:center; } h1{ color : blue; } </style> </head> <body> <h1> This is your first Mini powered page </h1> </body> </html> """ response = HttpResponse(template) response.mimetype = "text/html" response.status = "200 OK" response.update_headers("X-Auth", "67bd6bd7") return response # app.add_route("/check", check) # app.add_route("/red", red) app.add_route("/", welcome) app2.add_route("/", red) app2.add_route("welcome/", welcome) app.mount_wsgi_app("app2/", app2) # print(app.get_static_url('script.ts')) # app.run('',9000)