Skip to content

NodeHttpServer.ts

Node.js implementation of the Effect HttpServer.

This module adapts a supplied Node http.Server into Effect’s platform-independent HTTP server service. It starts the server with Node listen options, converts request events into HttpServerRequest values, writes HttpServerResponse bodies through Node’s ServerResponse, and handles upgrade events by exposing the upgraded socket through HttpServerRequest.upgrade. It also exports request and upgrade handler constructors plus layers for the server alone, HTTP support services, the combined server, configurable options, and tests.

Since v4.0.0



Creates a scoped HttpServer from a Node http.Server, starts listening with the supplied options, registers request and upgrade handling, and closes the server during scope finalization with optional graceful-shutdown control.

Signature

declare const make: (
evaluate: LazyArg<Http.Server<typeof Http.IncomingMessage, typeof Http.ServerResponse>>,
options: Net.ListenOptions & {
readonly disablePreemptiveShutdown?: boolean | undefined
readonly gracefulShutdownTimeout?: Duration.Input | undefined
}
) => Effect.Effect<
{
readonly serve: {
<E, R>(
effect: Effect.Effect<HttpServerResponse, E, R>
): Effect.Effect<void, never, 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>
): Effect.Effect<void, never, Exclude<R, HttpServerRequest> | Scope.Scope>
}
readonly address: HttpServer.Address
},
ServeError,
Scope.Scope
>

Source

Since v4.0.0

Creates a Node request event handler for an Effect HTTP application, injecting a HttpServerRequest and interrupting the request fiber if the client closes the response before it finishes.

Signature

declare const makeHandler: <
R,
E,
App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<HttpServerResponse, E, R>
>(
httpEffect: Effect.Effect<HttpServerResponse, E, R>,
options: {
readonly scope: Scope.Scope
readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
}
) => Effect.Effect<
(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void,
never,
Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
>

Source

Since v4.0.0

Creates a Node upgrade event handler for an Effect HTTP application, exposing the upgraded WebSocket as the request’s upgrade effect and interrupting the request fiber when the socket closes early.

Signature

declare const makeUpgradeHandler: <
R,
E,
App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<HttpServerResponse, E, R>
>(
lazyWss: Effect.Effect<NodeWS.WebSocketServer>,
httpEffect: Effect.Effect<HttpServerResponse, E, R>,
options: {
readonly scope: Scope.Scope
readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
}
) => Effect.Effect<
(nodeRequest: Http.IncomingMessage, socket: Duplex, head: Buffer) => void,
never,
Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
>

Source

Since v4.0.0

Provides a Node HttpServer together with the Node HTTP platform, ETag, and core platform services required to serve requests.

Signature

declare const layer: (
evaluate: LazyArg<Http.Server>,
options: Net.ListenOptions & {
readonly disablePreemptiveShutdown?: boolean | undefined
readonly gracefulShutdownTimeout?: Duration.Input | undefined
}
) => Layer.Layer<
HttpServer.HttpServer | NodeServices.NodeServices | HttpPlatform.HttpPlatform | Etag.Generator,
ServeError
>

Source

Since v4.0.0

Provides a Node HttpServer together with the Node HTTP platform, ETag, and core Node platform services, reading the listen and shutdown options from a Config value.

Signature

declare const layerConfig: (
evaluate: LazyArg<Http.Server>,
options: Config.Wrap<
Net.ListenOptions & {
readonly disablePreemptiveShutdown?: boolean | undefined
readonly gracefulShutdownTimeout?: Duration.Input | undefined
}
>
) => Layer.Layer<
HttpServer.HttpServer | NodeServices.NodeServices | HttpPlatform.HttpPlatform | Etag.Generator,
ServeError | Config.ConfigError
>

Source

Since v4.0.0

Provides the Node HTTP support services used by NodeHttpServer, including the HTTP platform, ETag generator, and core Node platform services.

Signature

declare const layerHttpServices: Layer.Layer<
NodeServices.NodeServices | HttpPlatform.HttpPlatform | Etag.Generator,
never,
never
>

Source

Since v4.0.0

Provides an HttpServer by creating and managing a scoped Node http.Server with the supplied listen and shutdown options.

Signature

declare const layerServer: (
evaluate: LazyArg<Http.Server<typeof Http.IncomingMessage, typeof Http.ServerResponse>>,
options: Net.ListenOptions & {
readonly disablePreemptiveShutdown?: boolean | undefined
readonly gracefulShutdownTimeout?: Duration.Input | undefined
}
) => Layer.Layer<HttpServer.HttpServer, ServeError>

Source

Since v4.0.0

Provides a test HTTP server listening on an ephemeral port together with a Fetch-backed HttpClient configured for server integration tests.

Signature

declare const layerTest: Layer.Layer<
FileSystem.FileSystem | Path.Path | HttpServer.HttpServer | HttpPlatform.HttpPlatform | Etag.Generator | HttpClient,
ServeError,
never
>

Source

Since v4.0.0