A handier wrapper around a server response

interface IResponse {
    body: any;
    headers: Readonly<IncomingHttpHeaders>;
    json: boolean;
    original: ServerResponse<IncomingMessage> | Http2ServerResponse;
    status: null | Partial<Readonly<Status>>;
    send(): Promise<void>;
    setData(data: any): void;
    setHeaders(headers?: Readonly<IncomingHttpHeaders>): Readonly<IncomingHttpHeaders>;
}

Properties

body: any

The body of the response.

If json is explicitly set to true, it will be serialized into JSON (and content-type header will be set to application/json). It will also be if json is not explicitly set but the body value is not a string, not a Buffer, and not null either. After that, the result will eventually be converted to a Buffer to be sent.

headers: Readonly<IncomingHttpHeaders>

The currently set headers. Use setHeaders to change them.

json: boolean

Whether the body field should be serialized into JSON (and content-type header should be set to application/json).

When not explicitly set, the returned value will still be true if the body value is not a string, not a Buffer, and not null either.

original: ServerResponse<IncomingMessage> | Http2ServerResponse

The original Node.js object representing the response

status: null | Partial<Readonly<Status>>

An object {code, message}, where each property is optional. If code is never given, a default value of 200 is applied.

Methods

  • Sends the response, applying some data previously specified but not set yet

    Returns Promise<void>

  • A convenient method to set the body value and set json to true.

    Parameters

    • data: any

      json object to use as the response body

    Returns void

  • Merge given headers map with the previously set headers (initial set is an empty map).

    Parameters

    • Optionalheaders: Readonly<IncomingHttpHeaders>

      Headers to set. Each header value can be a number, a string, or an array of strings. Put a null value to suppress a header

    Returns Readonly<IncomingHttpHeaders>