HttpServer.ts
HttpServer.ts overview
Section titled “HttpServer.ts overview”Service for serving Effect HTTP responses on a concrete HTTP server.
Platform adapters provide HttpServer, and routers or applications consume
it to run an HttpServerResponse effect for each incoming request. The
service exposes the listening address, while this module also includes helpers
for address formatting, server logging, and clients that target the current
server in tests.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”accessors
Section titled “accessors”Creates a layer that starts serving an HTTP response effect with the current
HttpServer.
Details
The request service is supplied by the server for each request; the returned layer still requires the server, a scope, and any non-request dependencies of the response effect or middleware.
Signature
declare const serve: { (): <E, R>( effect: Effect.Effect<HttpServerResponse, E, R> ) => Layer.Layer<never, never, HttpServer | Exclude<R, HttpServerRequest | Scope.Scope>> <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>( middleware: Middleware.HttpMiddleware.Applied<App, E, R> ): ( effect: Effect.Effect<HttpServerResponse, E, R> ) => Layer.Layer<never, never, HttpServer | Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>> <E, R>( effect: Effect.Effect<HttpServerResponse, E, R> ): Layer.Layer<never, never, HttpServer | Exclude<R, HttpServerRequest | Scope.Scope>> <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>( effect: Effect.Effect<HttpServerResponse, E, R>, middleware: Middleware.HttpMiddleware.Applied<App, E, R> ): Layer.Layer<never, never, HttpServer | Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>>}Since v4.0.0
serveEffect
Section titled “serveEffect”Effect that starts serving an HTTP response effect with the current
HttpServer.
Details
The request service is supplied by the server for each request; the effect requires a scope and any non-request dependencies of the response effect or middleware.
Signature
declare const serveEffect: { (): <E, R>( effect: Effect.Effect<HttpServerResponse, E, R> ) => Effect.Effect<void, never, Scope.Scope | HttpServer | Exclude<R, HttpServerRequest>> <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>( middleware: Middleware.HttpMiddleware.Applied<App, E, R> ): ( effect: Effect.Effect<HttpServerResponse, E, R> ) => Effect.Effect<void, never, Scope.Scope | HttpServer | Exclude<Effect.Services<App>, HttpServerRequest>> <E, R>( effect: Effect.Effect<HttpServerResponse, E, R> ): Effect.Effect<void, never, Scope.Scope | HttpServer | Exclude<R, HttpServerRequest>> <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>( effect: Effect.Effect<HttpServerResponse, E, R>, middleware: Middleware.HttpMiddleware.Applied<App, E, R> ): Effect.Effect<void, never, Scope.Scope | HttpServer | Exclude<Effect.Services<App>, HttpServerRequest>>}Since v4.0.0
address
Section titled “address”Address (type alias)
Section titled “Address (type alias)”Address where an HTTP server is listening.
Details
The address is either a TCP host and port or a Unix domain socket path.
Signature
type Address = UnixAddress | TcpAddressSince v4.0.0
TcpAddress (interface)
Section titled “TcpAddress (interface)”TCP address for an HTTP server, identified by hostname and port.
Signature
export interface TcpAddress { readonly _tag: "TcpAddress" readonly hostname: string readonly port: number}Since v4.0.0
UnixAddress (interface)
Section titled “UnixAddress (interface)”Unix domain socket address for an HTTP server.
Signature
export interface UnixAddress { readonly _tag: "UnixAddress" readonly path: string}Since v4.0.0
addressFormattedWith
Section titled “addressFormattedWith”Reads the current server address, formats it with formatAddress, and passes
the formatted address to the supplied effectful function.
Signature
declare const addressFormattedWith: <A, E, R>( f: (address: string) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, HttpServer | R>Since v4.0.0
formatAddress
Section titled “formatAddress”Formats a server address as a display string.
Details
TCP addresses are formatted as http://host:port; Unix socket addresses are
formatted as unix://path.
Signature
declare const formatAddress: (address: Address) => stringSince v4.0.0
logAddress
Section titled “logAddress”Logs the formatted address of the current HTTP server.
Signature
declare const logAddress: Effect.Effect<void, never, HttpServer>Since v4.0.0
withLogAddress
Section titled “withLogAddress”Adds address logging to a layer that provides an HttpServer.
Signature
declare const withLogAddress: <A, E, R>(layer: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R | Exclude<HttpServer, A>>Since v4.0.0
constructors
Section titled “constructors”Constructs an HttpServer service from a serving implementation and listening
address.
Signature
declare const make: (options: { readonly serve: ( httpEffect: Effect.Effect<HttpServerResponse, unknown, HttpServerRequest | Scope.Scope>, middleware?: Middleware.HttpMiddleware ) => Effect.Effect<void, never, Scope.Scope> readonly address: Address}) => HttpServer["Service"]Since v4.0.0
models
Section titled “models”HttpServer (class)
Section titled “HttpServer (class)”Service tag for an HTTP server runtime.
Details
The service can serve an HTTP response effect and exposes the address where the server is listening.
Signature
declare class HttpServerSince v4.0.0
testing
Section titled “testing”layerServices
Section titled “layerServices”Layer that provides the platform services commonly needed by HTTP server tests.
Details
It includes HttpPlatform, Path, a weak ETag generator, and a no-op
FileSystem.
Signature
declare const layerServices: Layer.Layer< Path.Path | FileSystem.FileSystem | Etag.Generator | HttpPlatform.HttpPlatform, never, never>Since v4.0.0
layerTestClient
Section titled “layerTestClient”Layer that provides the test HttpClient created by makeTestClient.
Signature
declare const layerTestClient: Layer.Layer<HttpClient.HttpClient, never, HttpClient.HttpClient | HttpServer>Since v4.0.0
makeTestClient
Section titled “makeTestClient”Builds an HttpClient that sends requests to the current test HTTP server.
Details
For TCP servers, requests are prefixed with the server URL and 0.0.0.0 is
rewritten to 127.0.0.1.
Gotchas
Unix socket addresses are not supported.
Signature
declare const makeTestClient: Effect.Effect<HttpClient.HttpClient, never, HttpClient.HttpClient | HttpServer>Since v4.0.0