nestrs::prelude::*.
A typical controller looks like this:
#[controller]
Marks a struct as a nestrs controller. Sets the route prefix and optional API version for all routes registered in the associated #[routes] impl block.
URL prefix applied to all routes in this controller. May include path segments with leading slash.
Route version string used by route-level versioning (e.g.,
"v1"). Works together with #[ver] on individual handlers.#[routes]
Expands an impl block on a controller struct into Axum route registrations. Must appear on the impl block directly following a #[controller]-annotated struct.
The Axum application state type injected into handler parameters. Must implement
Clone and be registered as a provider.HTTP method macros
All HTTP method macros accept a path string as their argument and are applied to async methods inside a#[routes] impl block.
| Macro | HTTP method |
|---|---|
#[get("path")] | GET |
#[post("path")] | POST |
#[put("path")] | PUT |
#[patch("path")] | PATCH |
#[delete("path")] | DELETE |
#[options("path")] | OPTIONS |
#[head("path")] | HEAD |
#[all("path")] | All methods (wildcard) |
/:param for path parameters, /*wildcard for catch-all segments.
Parameter extraction attributes
Inside a#[routes] impl block, method parameters can be annotated to declare their extraction source:
| Attribute | Axum equivalent | Notes |
|---|---|---|
#[param::body] | Json<T> | JSON request body; pair with #[use_pipes(ValidationPipe)] for validation |
#[param::query] | Query<T> | URL query string; pair with #[use_pipes(ValidationPipe)] |
#[param::param] | Path<T> | URL path parameters; pair with #[use_pipes(ValidationPipe)] |
#[param::headers] | HeaderMap | Raw request headers |
#[param::req] | Request | Full Axum request object |
#[param::ip] | ClientIp | Best-effort client IP extraction |
Response shaping macros
#[http_code]
Sets the HTTP status code for a successful handler response.
#[response_header]
Adds a response header to the handler’s response.
#[redirect]
Redirects the request to a different URL.
Route versioning macros
#[ver] / #[version]
Overrides the version for an individual route handler. Works in combination with the version set on #[controller].
#[raw_body]
Marks a handler parameter to receive the raw request body bytes instead of a deserialized value.
#[sse]
Marks a handler as a Server-Sent Events endpoint. The handler return type must implement the SSE stream interface from nestrs::sse.
Cross-cutting handler attributes
These attributes apply pipeline steps to individual route handlers and can be combined:| Attribute | Purpose |
|---|---|
#[use_guards(GuardType)] | Run GuardType::can_activate before the handler |
#[use_interceptors(InterceptorType)] | Wrap the handler with InterceptorType::intercept |
#[use_pipes(ValidationPipe)] | Transform/validate parameters before the handler |
#[use_filters(FilterType)] | Catch HttpException values after the handler |
impl_routes! macro
impl_routes! is the lower-level macro that #[routes] expands into. You can use it directly for explicit route registration without proc macro expansion on the impl block.
The
impl_routes! macro also accepts controller_guards (G) to apply a guard to all routes in the block, running outside route-level guards in the pipeline order.