Skip to content

NodeHttpServer

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.

9 exports Added in v4.0.0 Source

Constructors

make

Added in v4.0.0 Source

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: (...args: [evaluate: LazyArg<Server>, options: Options]) => Effect<
  {
    readonly address: Address;
    readonly serve: {
      <E, R>(
        effect: Effect<HttpServerResponse, E, R>,
      ): Effect<void, never, Scope | Exclude<R, HttpServerRequest>>;
      <E, R, App extends Effect<HttpServerResponse, any, any>>(
        effect: Effect<HttpServerResponse, E, R>,
        middleware: Applied<App, E, R>,
      ): Effect<void, never, Scope | Exclude<R, HttpServerRequest>>;
    };
  },
  ServeError,
  Scope
>;

Handlers

makeHandler

Added in v4.0.0 Source

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 function makeHandler<
  R,
  E,
  App extends Effect<HttpServerResponse, any, any> = Effect<HttpServerResponse, E, R>,
>(
  httpEffect: Effect<HttpServerResponse, E, R>,
  options: {
    readonly middleware?: Applied<App, E, R>;
    readonly scope: Scope;
  },
): Effect<
  (nodeRequest: IncomingMessage, nodeResponse: ServerResponse) => void,
  never,
  Exclude<Services<App>, Scope | HttpServerRequest>
>;

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 function makeUpgradeHandler<
  R,
  E,
  App extends Effect<HttpServerResponse, any, any> = Effect<HttpServerResponse, E, R>,
>(
  lazyWss: Effect<WebSocketServer>,
  httpEffect: Effect<HttpServerResponse, E, R>,
  options: {
    readonly middleware?: Applied<App, E, R>;
    readonly scope: Scope;
  },
): Effect<
  (nodeRequest: IncomingMessage, socket: Duplex, head: Buffer) => void,
  never,
  Exclude<Services<App>, Scope | HttpServerRequest>
>;

Layers

layer

Added in v4.0.0 Source

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

Signature

declare function layer(
  evaluate: LazyArg<Server>,
  options: Options,
): Layer<HttpPlatform | Generator | NodeServices | HttpServer, ServeError>;

layerConfig

Added in v4.0.0 Source

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 function layerConfig(
  evaluate: LazyArg<Server>,
  options:
    | {
        readonly disablePreemptiveShutdown?: Config<boolean | undefined>;
        readonly gracefulShutdownTimeout?: Config<Input | undefined>;
        readonly websocket?: Config<
          Omit<ServerOptions, "noServer" | "server" | "host" | "port" | "path"> | undefined
        >;
      }
    | Config<Options>,
): Layer<HttpPlatform | Generator | NodeServices | HttpServer, ConfigError | ServeError>;

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
>;

layerServer

Added in v4.0.0 Source

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: Options,
) => Layer.Layer<HttpServer.HttpServer, ServeError>;

Options

Options interface

Added in v4.0.0 Source

Options accepted by the Node HttpServer constructors and layers.

Signature

interface Options extends unknown {
  readonly disablePreemptiveShutdown?: boolean;
  readonly gracefulShutdownTimeout?: Input;
  readonly websocket?: Omit<ServerOptions, "noServer" | "server" | "host" | "port" | "path">;
}

Testing

layerTest

Added in v4.0.0 Source

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<
  | HttpServer.HttpServer
  | FileSystem.FileSystem
  | Path.Path
  | HttpPlatform.HttpPlatform
  | Etag.Generator
  | HttpClient,
  ServeError,
  never
>;