# 路由
  用戶可以通過路由來設置URL與handler之間的關聯。一個基本的路由形式如下:
```python
from sanic import response
@app.route("/")
async def test(request):
return response.text('Hello World')
```
  sanic的handler方法必須用`async`關鍵字定義為異步方法。
## Request參數
  request路由規則與flask一致,可以使用變量、正則來設置。
```python
from sanic.response import text
# 參數名: tag,可以是任意字符
@app.route('/tag/<tag>')
async def tag_handler(request, tag):
return text('Tag - {}'.format(tag))
# 參數名: integer_arg,int類型
@app.route('/number/<integer_arg:int>')
async def integer_handler(request, integer_arg):
return text('Integer - {}'.format(integer_arg))
@app.route('/number/<number_arg:number>')
async def number_handler(request, number_arg):
return text('Number - {}'.format(number_arg))
# 參數名: name,英文字母
@app.route('/person/<name:[A-z]+>')
async def person_handler(request, name):
return text('Person - {}'.format(name))
# 參數名: folder_id,英文字母+數字,0~4個字符長度
@app.route('/folder/<folder_id:[A-z0-9]{0,4}>')
async def folder_handler(request, folder_id):
return text('Folder - {}'.format(folder_id))
```
## HTTP請求類型
  默認請求類型是GET,如果用其它方法,需要單獨指定。
```python
from sanic.response import text
# post方法
@app.route('/post', methods=['POST'])
async def post_handler(request):
return text('POST request - {}'.format(request.json))
# get方法
@app.route('/get')
async def get_handler(request):
return text('GET request - {}'.format(request.args))
```
  此外,還可以指定host等額外參數。
```python
@app.route('/get', methods=['GET'], host='example.com')
async def get_handler(request):
return text('GET request - {}'.format(request.args))
# 如果host頭不匹配example.com,會自動使用這條路由
@app.route('/get')
async def get_handler(request):
return text('GET request in default - {}'.format(request.args))
```
## add_route方法
  如上文所示,通常使用`app.route`裝飾器來定義路由規則,實際上這個裝飾器封裝了`app.add_route`方法,所以可以像下面形式添加路由。
```python
from sanic.response import text
# handler方法
async def handler1(request):
return text('OK')
async def handler2(request, name):
return text('Folder - {}'.format(name))
async def person_handler2(request, name):
return text('Person - {}'.format(name))
# 為每個方法添加路由
app.add_route(handler1, '/test')
app.add_route(handler2, '/folder/<name>')
app.add_route(person_handler2, '/person/<name:[A-z]>', methods=['GET'])
```
  這種方式可以在文件中統一管理路由,代碼更清晰。
## 使用url_for創建URL
  handler方法內可以使用`url_for`來創建URL。通過引用handler名字來構建,避免在代碼中硬編碼URL。
```python
@app.route('/')
async def index(request):
# 生成一個post_handler的url
url = app.url_for('post_handler', post_id=5)
# URL是`/posts/5`
url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
# /posts/5?arg_one=one&arg_two=two
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
# /posts/5?arg_one=one&arg_one=two
return redirect(url)
@app.route('/posts/<post_id>')
async def post_handler(request, post_id):
return text('Post - {}'.format(post_id))
```
### 特殊參數
- _anchor 錨點
- _method 現在版本還不支持
- _server 域名和端口號
```python
url = app.url_for('post_handler', post_id=5, arg_one='one', _anchor='anchor')
# /posts/5?arg_one=one#anchor
# you can pass all special arguments one time
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'], arg_two=2, _anchor='anchor', _server='another_server:8888')
# http://another_server:8888/posts/5?arg_one=one&arg_one=two&arg_two=2#anchor
```