Routing
HTTP routing is the process of directing an incoming request to a handler function in your web application.
To register a route handler, call methods named after HTTP verbs, with a path and a handler as arguments.
const app = new Byte();
app.get('/', (ctx) => ctx.text('Hi'));app.post('/json', (ctx) => ctx.json({ message: 'Hello World!' }));To handle all methods using a handler, register the handle with any method:
app.any('/', (ctx) => ctx.body('Hi'));Patterns
Basic route patterns are supported.
URL parameters
Capture the value of a part separated by slash characters of the pathname.
app.get('/user/:id', (ctx) => ctx.body(ctx.params.id));Example cases:
/user/123: Return123./user/456/info: Does not match the pattern./user: Does not match the pattern.
Wildcards
Capture a slice from a specific part of a pathname.
app.get('/user/*', (ctx) => ctx.body(ctx.params.$));Example cases:
/user/123: Return123./user/456/info: Return456/info./user: Does not match the pattern.
The type of ctx.params is inferred automatically from the registered pattern.
Sub-routers
To register all handlers from other Byte instance:
const api = new Byte() .get('/', (ctx) => ctx.body('Hi'));
const app = new Byte() .route('/api', api);