Utils

Response

Utilities to send response headers and data


appendHeader(event, name, value)

Append a response header by name.

appendHeaders(event, headers)

Append the response headers.

appendResponseHeader(event, name, value)

Append a response header by name.

appendResponseHeaders(event, headers)

Append the response headers.

clearResponseHeaders(event, headerNames?)

Remove all response headers, or only those specified in the headerNames array.

defaultContentType(event, type?)

Set the response status code and message.

getResponseHeader(event, name)

Alias for getResponseHeaders.

getResponseHeaders(event)

Get the response headers object.

getResponseStatus(event)

Get the current response status code.

getResponseStatusText(event)

Get the current response status message.

isStream(data)

Checks if the data is a stream. (Node.js Readable Stream, React Pipeable Stream, or Web Stream)

isWebResponse(data)

Checks if the data is a Response object.

removeResponseHeader(event, name)

Remove a response header by name.

send(event, data?, type?)

Directly send a response to the client.

Note: This function should be used only when you want to send a response directly without using the h3 event. Normaly you can directly return a value inside event handlers.

sendIterable(event, iterable)

Iterate a source of chunks and send back each chunk in order. Supports mixing async work toghether with emitting chunks.

Each chunk must be a string or a buffer.

For generator (yielding) functions, the returned value is treated the same as yielded values.

Example:

sendIterable(event, work());
async function* work() {
  // Open document body
  yield "<!DOCTYPE html>\n<html><body><h1>Executing...</h1><ol>\n";
  // Do work ...
  for (let i = 0; i < 1000) {
    await delay(1000);
    // Report progress
    yield `<li>Completed job #`;
    yield i;
    yield `</li>\n`;
  }
  // Close out the report
  return `</ol></body></html>`;
}
async function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

sendNoContent(event, code?)

Respond with an empty payload.

Note that calling this function will close the connection and no other data can be sent to the client afterwards.

sendRedirect(event, location, code)

Send a redirect response to the client.

It adds the location header to the response and sets the status code to 302 by default.

In the body, it sends a simple HTML page with a meta refresh tag to redirect the client in case the headers are ignored.

sendStream(event, stream)

Send a stream response to the client.

Note: You can directly return a stream value inside event handlers alternatively which is recommended.

sendWebResponse(event, response)

Send a Response object to the client.

setHeader(event, name, value)

Set a response header by name.

setHeaders(event)

Set the response headers.

setResponseHeader(event, name, value)

Set a response header by name.

setResponseHeaders(event)

Set the response headers.

setResponseStatus(event, code?, text?)

Set the response status code and message.

writeEarlyHints(event, hints, cb)

Write HTTP/1.1 103 Early Hints to the client.