Skip to content

Set response status, body and headers ​

Return an object for a conventional JSON response. Return a Fetch Response when the client needs a specific status or header.

Handler resultResponse
{ id: "42" }, an array, number, boolean or nullJSON, status 200
A stringText with UTF-8 content type, status 200
undefined, including a method with no returnEmpty body, status 204
Uint8Array, ArrayBuffer, other array views, Blob, FormData, URLSearchParams or ReadableStreamFetch body with the platform's applicable headers
A ResponseIts status, headers and body

CHO awaits a promise before applying these rules. Check for a missing record explicitly. Returning undefined from a lookup would send 204 to the client.

Report creation ​

ts
@Post("")
@Args(Body("title", titleValidator))
create(title: string) {
  const book = this.catalog.add(title);
  return Response.json(book, {
    status: 201,
    headers: { location: "/books/" + encodeURIComponent(book.id) },
  });
}

In this controller fragment, titleValidator runs under validate() and catalog is an injected store. Set Location to the actual public path, including any application prefix.

Stream or download a body ​

Wrap a byte stream in new Response(stream, { headers }) to choose its media type. For a browser download, also set Content-Disposition. For typed event streams, @Sse handles event framing and response headers. Stream bytes · Download a file · Server-sent events

Released under the MIT License.