Skip to content

Latest commit

 

History

History
93 lines (61 loc) · 1.24 KB

README.md

File metadata and controls

93 lines (61 loc) · 1.24 KB

xweb-router

Router middleware for xweb

Usage

from xweb import App

from xweb_router import Router

app = App()
router = Router()
app.use(router)

@router.get('/')
async def home(ctx):
    ctx.body = "Home"

if __name__ == '__main__':
    app.listen(8000)

MiddleWare

from xweb import App

from xweb_router import Router

app = App()
router = Router()
app.use(router)


@router.use('/')
async def middleware(ctx, fn):
    """Router Middleware"""
    print('middleware')
    await fn()
    
@router.get('/')
async def home(ctx):
    ctx.body = "Home"
    
if __name__ == '__main__':
    app.listen(8000)

Router Parameters

from xweb import App

from xweb_router import Router

app = App()
router = Router()

@router.get('/{name}')
async def hello(ctx):
    """URL parameters"""
    ctx.body = f"Hello {ctx.params.name}"

if __name__ == '__main__':
    app.listen(8000)

Nested Router

from xweb import App

from xweb_router import Router

app = App()
router = Router()
nested = Router()

app.use(router)

router.use('/post')(nested)


@nested.get('/index')
async def index(ctx):
    ctx.body = "Nested Index"


if __name__ == '__main__':
    app.listen(8000)