2016-10-14 12:51:08 +01:00
# Routing
2016-10-15 19:30:44 +01:00
Sanic comes with a basic router that supports request parameters. To specify a parameter, surround it with carrots like so: `<PARAM>` . Request parameters will be passed to the request handler functions as keyword arguments. To specify a type, add a :type after the parameter name, in the carrots. If the parameter does not match the type supplied, Sanic will throw a NotFound exception, resulting in a 404 page not found error.
2016-10-14 12:51:08 +01:00
## Examples
```python
from sanic import Sanic
2016-10-14 12:58:09 +01:00
from sanic.response import text
2016-10-14 12:51:08 +01:00
@app .route('/tag/< tag > ')
2016-10-21 22:47:13 +01:00
async def tag_handler(request, tag):
2016-10-14 12:51:08 +01:00
return text('Tag - {}'.format(tag))
@app .route('/number/< integer_arg:int > ')
2016-10-21 22:47:13 +01:00
async def integer_handler(request, integer_arg):
2016-10-14 12:51:08 +01:00
return text('Integer - {}'.format(integer_arg))
@app .route('/number/< number_arg:number > ')
2016-10-21 22:47:13 +01:00
async def number_handler(request, number_arg):
2016-10-18 13:13:37 +01:00
return text('Number - {}'.format(number_arg))
2016-10-14 12:51:08 +01:00
2016-10-15 19:03:05 +01:00
@app .route('/person/< name: [ A-z ] > ')
async def person_handler(request, name):
return text('Person - {}'.format(name))
2016-10-14 12:51:08 +01:00
2016-10-15 19:03:05 +01:00
@app .route('/folder/< folder_id: [ A-z0-9 ]{ 0 , 4 } > ')
2016-10-14 12:51:08 +01:00
async def folder_handler(request, folder_id):
return text('Folder - {}'.format(folder_id))
2016-11-25 07:10:25 +00:00
async def handler1(request):
return text('OK')
app.add_route(handler1, '/test')
2016-12-29 17:22:11 +00:00
async def handler2(request, name):
2016-11-25 07:10:25 +00:00
return text('Folder - {}'.format(name))
2016-12-29 17:22:11 +00:00
app.add_route(handler2, '/folder/< name > ')
2016-11-25 07:10:25 +00:00
2016-12-29 17:22:11 +00:00
async def person_handler2(request, name):
2016-11-25 07:10:25 +00:00
return text('Person - {}'.format(name))
2016-12-29 17:22:11 +00:00
app.add_route(person_handler2, '/person/< name: [ A-z ] > ')
2016-11-25 07:10:25 +00:00
2016-10-15 19:30:44 +01:00
```