You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorials.rst
+17-2Lines changed: 17 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,12 @@ Chapter 2: Static File Server
61
61
=============================
62
62
Although you might want your App to be as dynamic as possible, you have to first understand how a static website is served.
63
63
64
+
The convention is to have a :code:`public` directory under your project directory and have all your static files there.
65
+
66
+
For example, if the client does :code:`GET /some_file.format`, the framework will try to read the path :code:`public/some_file.format` and sent what it read to the client. If the path does not exist, then the framework simply reports resource not found.
67
+
68
+
Therefore, you can easily just put all your frontend code under the public directory and start serving your application.
69
+
64
70
Chapter 3: REST
65
71
===============
66
72
Web is built on HTTP and HTTP is all about semantics. While there are thousands of ways to build HTTP API, the one which our framework embraces is REST. Since our audience's experience varies, I do not want to confuse you by explaining too much about REST. The only thing you need to know is that HTTP requests are all about semantics and a REST API is a semantic API.
@@ -73,13 +79,22 @@ First, let's see the frontend code:
73
79
74
80
fetch('/top10', {method:'GET'});
75
81
76
-
:code:`fetch()` is the new browser API which does async HTTP requests. It is better than :code:`XMLHttpRequest` in almost every aspects. It has a cleaner interface which super fits REST.
82
+
:code:`fetch()` is the new browser API which does async HTTP requests. It is better than :code:`XMLHttpRequest` in almost every aspects. It has a cleaner interface which fits RESTful API design.
77
83
78
84
This line creates a HTTP GET request with :code:`:path` = :code:`/top10`. How to respond to this request is 100% up to the server. Now, in :code:`app.py`, write this piece of code:
79
85
80
86
.. code-block:: Python
81
87
82
-
To be continued...
88
+
# define API's callback function
89
+
asyncdeftop10_api(request, response):
90
+
await response.send("some data")
91
+
92
+
# register this function
93
+
app.get("top10", top10_api)
94
+
# you can also do
95
+
# app.get("/top10", top10_api)
96
+
97
+
Now, whenever the client does such a request, :code:`top10_api` will be run by the framework. We call this function the endpoint of a REST API. You can define the function as whatever you need, but have to include the :code:`async` and :code:`await` key words.
0 commit comments