Routing
Redirects & responses
A handler returns a value and the framework turns it into a response. Most of the time you return plain data and let the context shape it; when you need control, you set a status, build a response, or declare a redirect on the route itself.
#Returning data
Return an object or array and it becomes the body, serialised by the active context (JSON on an HTTP adapter, a rendered view in the browser). This is the default, and it keeps handlers platform-agnostic.
@Get('/:id')
show (event: IncomingHttpEvent) {
return this.tasks.find(event.get('id')) // -> serialised by the context
}#Controlling the response
When you need a specific status or headers, build a response with the HTTP layer's helpers. Throwing a domain error maps to the right status through the error handler, so the common cases stay declarative.
import { jsonHttpResponse } from '@stone-js/http-core'
@Post('/')
create (event: IncomingHttpEvent) {
const task = this.tasks.add(event.get('title'))
return jsonHttpResponse(task, 201) // explicit status
}#Redirects
A route can redirect without a handler at all, using the redirect option, or a handler can return a redirect response. Use the route option for static moves (renamed paths), the response for conditional ones.
// Static: declare it on the route, no handler needed.
@Get('/old-tasks', { redirect: '/tasks' })
legacy () {}
// Conditional: return a redirect from the handler.
@Post('/tasks')
create (event) {
const task = this.tasks.add(event.get('title'))
return reactRedirectResponse(`/tasks/${task.id}`)
}#Redirect options
| Form | Type | Description |
|---|---|---|
| redirect: '/path' | string | Redirect to a path (default status 302). |
| redirect: { location, status } | object | Redirect with an explicit status (301, 307, ...). |
| a redirect response | from the handler | Build the redirect at runtime, based on the outcome. |