HttpServer module provides abstractions for building HTTP servers with Effect. It offers a composable, type-safe way to define server applications with middleware support and automatic resource management.
Overview
HttpServer features:- Composable middleware architecture
- Automatic scope management
- Type-safe request/response handling
- Platform-agnostic abstractions
- Built-in static file serving
- Testing utilities
Basic Usage
Server Setup
serve
Creates a Layer that serves an HTTP application.
serveEffect
Serves an HTTP application as an Effect.
Address Information
Server Address Types
Servers can bind to TCP or Unix socket addresses:formatAddress
Formats a server address as a string.
logAddress
Logs the server address when it starts.
withLogAddress
Wraps a layer to log the server address on startup.
Creating Custom Servers
make
Creates a custom HttpServer implementation.
Request Handling
Accessing the Request
Request Properties
Response Creation
See the HttpServerResponse module for comprehensive response APIs:Middleware
Middleware allows composing cross-cutting concerns:Testing
makeTestClient
Creates an HttpClient configured to make requests to the test server.
layerTestClient
A Layer version of makeTestClient.
layerServices
Provides default platform services for testing.
Router Integration
HttpServer works seamlessly with HttpRouter for routing:Static Files
Serve static files with HttpStaticServer:WebSocket Support
Upgrade connections to WebSocket:Platform Implementations
HttpServer is platform-agnostic. Use platform-specific layers:Node.js
Bun
Complete Example
Type Reference
HttpServer
See Also
- HttpServerRequest - Request handling
- HttpServerResponse - Response creation
- HttpRouter - Request routing
- HttpMiddleware - Middleware composition
- Layer - Dependency injection
