使用蓝图的 `route()` 方法,通过添加装饰器的形式,就可以把一个函数绑定到对应的 URL 上。
```
@bp.route('/', methods=['GET'])
def index():
return 'hello flask'
```
为 `app/api_1_0/controller` 下的 `index.py` 控制器绑定路由 (我们以 `api_1_0` 模块为例进行演示)
```
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from app.api_1_0 import bp
@bp.route('/', methods=['GET'])
def index():
return 'hello flask'
```
在终端下进入 `flask_demo` 目录,并激活虚拟环境
```
# 以开发模式运行
(.venv) root@airvip:~/python_app/flask-demo# export FLASK\_ENV=development
# 指定入口文件
(.venv) root@airvip:~/python_app/flask-demo# export FLASK\_APP=manage.py
# 启动项目
(.venv) root@airvip:~/python_app/flask-demo# flask run
* Serving Flask app "manage.py" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 140-781-151
```
此时在浏览器访问 `http://127.0.0.1:5000/api/v1.0/` 即可得到如下页面
![flask_index](https://img.kancloud.cn/3f/92/3f92a2fbee07a5240a246b6ea4695471_475x127.png)
**练习**
为 `app/api_1_0/controller` 下的 `passport.py` 控制器绑定路由
```
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from app.api_1_0 import bp
@bp.route('/register', methods=['POST'])
def register():
pass
@bp.route('/login', methods=['POST'])
def login():
pass
@bp.route('/logout', methods=['DELETE'])
def logout():
pass
```