Routing
Groups & prefixes
Related routes usually share a prefix and a set of concerns: an admin area behind auth, an API version, a resource. A group states those once and applies them to every route inside, so you never repeat a prefix or a guard.
#The controller is a group
Declaratively, a controller is the group: its base path prefixes every method, and options set on @EventHandler (like middleware) apply to all of them. Imperatively, a parent entry with children does the same.
import { EventHandler, Get, Post } from '@stone-js/router'
import { requireAuth } from '@stone-js/auth'
// The controller is the group: a shared base path and shared options.
@EventHandler('/admin/tasks', { middleware: [requireAuth()] })
export class AdminTaskController {
@Get('/') list () { /* GET /admin/tasks (auth required) */ }
@Post('/') create () { /* POST /admin/tasks (auth required) */ }
@Get('/:id') show () { /* GET /admin/tasks/:id (auth required) */ }
}import { defineEventHandler, defineRoutes } from '@stone-js/router'
import { requireAuth } from '@stone-js/auth'
// A group: shared path prefix and middleware via a parent with children.
export const routes = defineRoutes([
[null, {
path: '/admin/tasks',
middleware: [requireAuth()],
children: [
[defineEventHandler(AdminTaskController, 'list'), { path: '/', method: 'GET' }],
[defineEventHandler(AdminTaskController, 'create'), { path: '/', method: 'POST' }],
[defineEventHandler(AdminTaskController, 'show'), { path: '/:id', method: 'GET' }]
]
}]
])#What a group shares
- Path prefix: each child path is appended to the group's base path.
- Middleware: runs for every route in the group (a child can opt out with
excludeMiddleware). - Constraints & defaults:
rulesanddefaultscascade to children. - Domain & protocol: restrict a whole group to a host or scheme.