@eryx/http/ServerResponse Module

JSON

A writable HTTP response used inside an HttpServer handler.

Supports both one-shot replies and streaming (chunked transfer-encoding). Trailers can also be attached to chunked responses.

One-shot:

res:send(200, "Hello!")

Streaming:

res:status(200):header("content-type", "text/plain")
res:write("chunk 1")
res:write("chunk 2")
res:finish()

Stream from a reader:

res:status(200)
res:trailer("x-checksum", "ok")
res:sendStream(myReader)

Summary

Classes

close: (((self: ResponseBodyStream) → ()))?
ResponseBodyStream:read(size: number?)string?
_sock: any
_headersSent: boolean
_finished: boolean
_upgraded: boolean
_status: number
_reason: string
_headers: { [string]: string }
_chunked: boolean
_requestMethod: string?
_requestHttpVersion: string?
_keepAlive: boolean
_holdOpen: boolean
_trailers: { [string]: string }
ServerResponse._new(sock: any, requestMethod: string?, requestHttpVersion: string?, requestHeaders: { [string]: string }?)ServerResponse
ServerResponse:status(code: number, reason: string?)ServerResponse
ServerResponse:header(name: string, value: string)ServerResponse
ServerResponse:headers(hdrs: { [string]: string })ServerResponse
ServerResponse:trailer(name: string, value: string)ServerResponse
ServerResponse:trailers(hdrs: { [string]: string })ServerResponse
ServerResponse:finish(body: string?)()
ServerResponse:sendStream(bodyStream: ResponseBodyStream, contentLength: number?)ServerResponse
ServerResponse:send(statusCode: number, body: string?, hdrs: { [string]: string }?)()
ServerResponse:json(statusCode: number, data: string)()

API Reference

Classes

ResponseBodyStream

Properties

close: (((self: ResponseBodyStream) → ()))?

ResponseBodyStream:read

ResponseBodyStream:read(size: number?)string?

ServerResponse

Properties

_sock: any
_headersSent: boolean
_finished: boolean
_upgraded: boolean
_status: number
_reason: string
_headers: { [string]: string }
_chunked: boolean
_requestMethod: string?
_requestHttpVersion: string?
_keepAlive: boolean
_holdOpen: boolean
_trailers: { [string]: string }

ServerResponse._new

ServerResponse._new(sock: any, requestMethod: string?, requestHttpVersion: string?, requestHeaders: { [string]: string }?)ServerResponse

ServerResponse:holdOpen

ServerResponse:holdOpen()ServerResponse

ServerResponse:status

Sets the HTTP status code and optional reason phrase.

Must be called before ServerResponse.write or ServerResponse.finish. Returns self for chaining.

ServerResponse:status(code: number, reason: string?)ServerResponse

Parameters

code: number

The HTTP status code (e.g. 200, 404).

reason: string?

Custom reason phrase. Defaults to the standard phrase for the code.

Returns

Self, for method chaining.

ServerResponse:header

Sets a single response header.

Must be called before ServerResponse.write or ServerResponse.finish. Returns self for chaining.

ServerResponse:header(name: string, value: string)ServerResponse

Parameters

name: string

The header name (case-insensitive).

value: string

The header value.

Returns

Self, for method chaining.

ServerResponse:headers

Sets multiple response headers at once.

Must be called before ServerResponse.write or ServerResponse.finish. Returns self for chaining.

ServerResponse:headers(hdrs: { [string]: string })ServerResponse

Parameters

hdrs: { [string]: string }

A table of header name/value pairs.

Returns

Self, for method chaining.

ServerResponse:trailer

Queues a trailer header to be sent after a chunked response body.

Trailers are only sent for chunked, body-bearing responses. The response automatically advertises the trailer names in the Trailer header.

ServerResponse:trailer(name: string, value: string)ServerResponse

Parameters

name: string

The trailer header name.

value: string

The trailer value.

Returns

Self, for method chaining.

ServerResponse:trailers

Queues multiple trailer headers to be sent after a chunked response.

ServerResponse:trailers(hdrs: { [string]: string })ServerResponse

Parameters

hdrs: { [string]: string }

Trailer name/value pairs.

Returns

Self, for method chaining.

ServerResponse:_sendHead

flush the status line + headers to the socket.

ServerResponse:_sendHead()()

ServerResponse:write

Writes a chunk of the response body.

Sends headers on the first call. Can be called multiple times for streaming or long-polling. Uses chunked transfer-encoding when no Content-Length is set.

ServerResponse:write(data: string)ServerResponse

Parameters

data: string

The body chunk to send.

Returns

Self, for method chaining.

ServerResponse:finish

Finishes the response.

Sends the terminating chunk if chunked transfer-encoding is active. If body is provided, it is a shorthand for write(body) followed by finish().

ServerResponse:finish(body: string?)()

Parameters

body: string?

Optional final body to send before finishing.

ServerResponse:sendStream

Streams a response body from an incremental reader.

This is useful for large downloads, generated content, or proxy-style handlers where buffering the entire body first would be wasteful.

If contentLength is omitted, the response uses chunked transfer encoding automatically.

ServerResponse:sendStream(bodyStream: ResponseBodyStream, contentLength: number?)ServerResponse

Parameters

bodyStream: ResponseBodyStream

The incremental body source.

contentLength: number?

Optional exact body size in bytes.

Returns

Self, for method chaining.

ServerResponse:send

Sends a complete response in one call (status + headers + body).

Convenience for the common case of a simple, non-streaming reply.

ServerResponse:send(statusCode: number, body: string?, hdrs: { [string]: string }?)()

Parameters

statusCode: number

The HTTP status code.

body: string?

The response body string.

hdrs: { [string]: string }?

Optional response headers.

ServerResponse:json

Sends a JSON response with the content-type header set to application/json.

ServerResponse:json(statusCode: number, data: string)()

Parameters

statusCode: number

The HTTP status code.

data: string

The JSON string to send as the body.

ServerResponse:wantsKeepAlive

ServerResponse:wantsKeepAlive()boolean