HTTP
The HTTP transport — controllers, routes, configuration, TLS, CORS.
NestRS’s HTTP layer accepts connections and routes requests itself, on
hyper; a handler reads its
request through nestrs’s own extractors and answers with
nestrs’s response types. A controller is a struct, routes come from
#[routes], and the transport activates with the module listing the controller
— the feature’s HTTP adapter — since #[routes] brings
HttpModule, with HttpModule::for_root(cfg) at the
app’s root only to pin its config: main never holds a transport.
Other HTTP-shaped surfaces — GraphQL, OpenAPI, MCP, WebSocket gateways —
all mount on the same HttpTransport via HttpEndpointMeta, so a single
binding (0.0.0.0:3002) serves them all. None of them runs its own port.
This category covers HTTP only — controllers, routes, response shaping, transport configuration. Authentication, authorization, and database queries each have their own category.
Install
Section titled “Install”cargo add nest-rs --features httpThat is the whole manifest for a controller. The http feature carries the
per-route guard chain, the pipe fold and the interceptor and exception-filter
sites that #[controller] and #[routes] expand to — none of which is a crate
you name.
In this section
Section titled “In this section”Basics
Section titled “Basics”- Controllers & routes —
#[controller],#[routes], path/query/JSON extractors, typed returns. - Responses —
Json<T>, status + body tuples,#[http_code],#[response_header],#[redirect]. - Errors —
ToProblemon a feature error enum, RFC 9457ProblemDetailsfor one-off problem responses. - Extractors — the full extractor surface:
Header<T>,RawBody,ClientIp,Scoped<T>andAmbient<T>.
All options
Section titled “All options”- Configuration —
HttpConfig, env vars vs pinned struct, TLS, the mount prefix, the frameworkServer:header. - CORS and security headers — the origins a browser page may call from, and the headers every response carries.
- Limits and deadlines — the body cap, the request budget, the connection cap and deadlines, the shutdown window.
- Problem details — an RFC 9457 document built
inline, and the
codeeach status carries. - Client IP — the peer, or the client a trusted proxy names, and what to list behind each proxy.
- Versioning —
#[controller(version = "1")]and multi-version controllers. - Compression — zstd, brotli, gzip or deflate negotiated from
Accept-Encoding, behind oneHttpConfigflag. - Streaming responses — a chunked body or a Server-Sent Events stream returned from a handler.
- File uploads — a
multipart/form-datapart streamed straight into object storage. - Static files — a built single-page app and public files, served from a directory or embedded in the binary.
Going further
Section titled “Going further”- OpenAPI — a self-composing OpenAPI 3.1 document and Swagger UI, read off the routes above.
- Security — bind an
AuthnGuardand anAbilityGuardto attach the principal and gate actions per row. - GraphQL, WebSockets, MCP — each
mounts on the same
HttpTransportyou configured here.