HttpStaticServer
Serves static files for Effect HTTP applications.
HttpStaticServer turns request paths into file responses under a configured
root directory. It can be used as an application value or mounted onto an
HttpRouter, and it handles index files, optional single-page application
fallback, MIME type headers, cache-control headers, byte ranges, and
conditional 304 Not Modified responses.
Constructors
Creates an HttpApp that serves files from a directory.
Signature
declare const make: (options: { readonly cacheControl?: string; readonly index?: string; readonly mimeTypes?: Record<string, string>; readonly root: string; readonly spa?: boolean;}) => Effect.Effect<Effect.Effect<HttpServerResponse.HttpServerResponse, HttpServerError.HttpServerError, HttpServerRequest.HttpServerRequest>, PlatformError, FileSystem.FileSystem | Path.Path | HttpPlatform.HttpPlatform>Example
(Serving files from a directory)
import { Effect, FileSystem, Layer, Path } from "effect"import { HttpEffect, HttpPlatform, HttpServerResponse, HttpStaticServer} from "effect/unstable/http"
const TestFileSystem = FileSystem.layerNoop({ stat: () => Effect.succeed({ type: "File", size: FileSystem.Size(20) } as FileSystem.File.Info)})const TestHttpPlatform = Layer.succeed( HttpPlatform.HttpPlatform, HttpPlatform.HttpPlatform.of({ platform: "web", fileResponse: (path) => Effect.succeed(HttpServerResponse.text(`Serving ${path}`)), fileWebResponse: () => Effect.die("unused") }))const TestServices = Layer.mergeAll(Path.layer, TestFileSystem, TestHttpPlatform)
const program = Effect.gen(function*() { const app = yield* HttpStaticServer.make({ root: "/public" }) const handler = HttpEffect.toWebHandler(app) const response = yield* Effect.promise(() => handler(new Request("http://localhost/guide.txt"))) const body = yield* Effect.promise(() => response.text()) return body}).pipe(Effect.provide(TestServices))
await Effect.runPromise(program) // => "Serving /public/guide.txt"Layers
Creates a layer that mounts static files on an HttpRouter.
Signature
declare function layer(options: { readonly cacheControl?: string; readonly index?: string; readonly mimeTypes?: Record<string, string>; readonly prefix?: string; readonly root: string; readonly spa?: boolean;}): Layer<never, PlatformError, FileSystem | Path | HttpRouter | HttpPlatform>Example
(Mounting static files on a router)
import { Layer } from "effect"import { HttpRouter, HttpServerResponse, HttpStaticServer } from "effect/unstable/http"
const ApiLayer = HttpRouter.add("GET", "/health", HttpServerResponse.text("ok"))
const StaticFilesLayer = HttpStaticServer.layer({ root: "./public", prefix: "/static"})
const AppLayer = Layer.mergeAll(ApiLayer, StaticFilesLayer)Layer.isLayer(AppLayer) // => true