Skip to content
NestRS

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.

Terminal window
cargo add nest-rs --features http

That 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.

  • Controllers & routes — #[controller], #[routes], path/query/JSON extractors, typed returns.
  • Responses — Json<T>, status + body tuples, #[http_code], #[response_header], #[redirect].
  • Errors — ToProblem on a feature error enum, RFC 9457 ProblemDetails for one-off problem responses.
  • Extractors — the full extractor surface: Header<T>, RawBody, ClientIp, Scoped<T> and Ambient<T>.
  • Configuration — HttpConfig, env vars vs pinned struct, TLS, the mount prefix, the framework Server: 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 code each 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 one HttpConfig flag.
  • Streaming responses — a chunked body or a Server-Sent Events stream returned from a handler.
  • File uploads — a multipart/form-data part streamed straight into object storage.
  • Static files — a built single-page app and public files, served from a directory or embedded in the binary.
  • OpenAPI — a self-composing OpenAPI 3.1 document and Swagger UI, read off the routes above.
  • Security — bind an AuthnGuard and an AbilityGuard to attach the principal and gate actions per row.
  • GraphQL, WebSockets, MCP — each mounts on the same HttpTransport you configured here.