REST
4.0.0 controllers use decorators from @heronjs/express, backed by native Fastify 5. Register controllers in a module so the application owns their runtime.
import 'reflect-metadata';
import {Module} from '@heronjs/common';
import {Rest, Get, Post, Body, Params} from '@heronjs/express';
@Rest('/todos')
export class TodoController {
@Get({uri: '/:id'})
findOne(@Params() params: {id: string}) {
return {id: params.id};
}
@Post({uri: '/', code: 201})
create(@Body() body: unknown) {
// Validate untrusted input before using it in application logic.
return {received: body};
}
}
@Module({controllers: [TodoController]})
export class AppModule {}Routes and parameters
Get, Post, Put, Patch and Delete accept route options including uri, code and headers.
| Decorator | Value |
|---|---|
@Params() | Route parameters |
@Queries() | Query parameters |
@Body() | Request body |
@Headers() | Request headers |
@Header(name) | One header (deprecated; prefer Headers) |
@Cookies() | Parsed cookies |
@Principal() | Authentication context |
@Request() | Native Fastify request |
@Response() | Native Fastify reply |
Type annotations do not validate network input. Validate request values at the boundary. Handlers may return values, Promises or Observables; unary routes use the first emission.
For manual responses, use reply.code(status).send(body) and reply.header(name, value). HeronJS avoids sending a second response when the injected reply has already sent one. Long-running work can observe request.executionSignal.
See Fastify configuration and Getting Started for server setup.
Last updated on