Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Response

RMResponse extends Node’s http.ServerResponse with the following additions.

Properties

res.redirect

Set to true inside processPost or createPath to make the response a 303 See Other redirect instead of the normal 2xx. The Location header must be set separately.

override async processPost() {
  const id = await createResource(this.req);
  this.res.setHeader("Location", this.req.baseURI(`/articles/${id}`));
  this.res.redirect = true;
  return true;
}

Methods

res.setBody(body)

Sets the response body. Accepted types:

TypeDescription
stringEncoded to bytes using the negotiated charset (default UTF-8)
BufferSent as-is
ReadablePiped to the response — enables streaming
// String
this.res.setBody("Hello, world!");

// JSON
this.res.setBody(JSON.stringify({ ok: true }));

// Stream
this.res.setBody(createReadStream("/path/to/file.txt"));

setBody is typically called inside a contentTypesProvided handler, though it can also be called directly inside processPost when the response body should be set as part of POST handling.

Body Providers

The more idiomatic approach is to return a body producer from contentTypesProvided. The return value of the producer is passed through encoding and charset transforms before being sent:

override async contentTypesProvided() {
  return {
    "application/json": () => JSON.stringify(this.data),
    "text/plain": () => `Name: ${this.data.name}`,
    "application/octet-stream": () => createReadStream(this.filePath),
  };
}

The producer can be sync or async — both are awaited before sending.