Skip to content

Commit 257320a

Browse files
Merge branch 'dev' of https://github.com/CreatCodeBuild/hyper2web into dev
2 parents 02baf73 + f6e5310 commit 257320a

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

docs/tutorials.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ Chapter 2: Static File Server
6161
=============================
6262
Although you might want your App to be as dynamic as possible, you have to first understand how a static website is served.
6363

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+
6470
Chapter 3: REST
6571
===============
6672
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:
7379
7480
fetch('/top10', {method: 'GET'});
7581
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.
7783

7884
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:
7985

8086
.. code-block:: Python
8187
82-
To be continued...
88+
# define API's callback function
89+
async def top10_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.
8398

8499
Chapter 4: Parameterized REST
85100
=============================

0 commit comments

Comments
 (0)