Effect HTTP Node.js Server

0.27.0 · active · verified Wed Apr 22

effect-http-node is a crucial adapter package within the `effect-ts` ecosystem, providing the Node.js-specific implementation for serving declarative HTTP APIs built with `effect-http`. It leverages the `@effect/platform-node` package to bridge `effect-http`'s server abstractions with Node.js's native HTTP server capabilities, enabling developers to run highly type-safe, functional, and concurrently managed web services. The current stable version is 0.27.0. This package, along with `effect-http` and core `effect` libraries, follows a rapid release cadence, often undergoing minor version bumps to align with updates in its peer dependencies, reflecting the active development of the `effect-ts` project. Its key differentiator lies in enabling the `effect-ts` programming model—emphasizing structured concurrency, resource management, and robust error handling—directly within Node.js for HTTP services, providing a strong alternative to traditional imperative frameworks. It does not introduce new HTTP API paradigms but rather provides the runtime environment for `effect-http`'s existing ones.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a simple `effect-http` API, implementing its handlers, and serving it using `effect-http-node` within a Node.js runtime. It includes a GET and a POST endpoint.

import { pipe, Effect } from 'effect';
import { Api, Router } from 'effect-http';
import { NodeServer } from 'effect-http-node';
import * as NodePlatform from '@effect/platform-node';
import * as Logger from '@effect/data/Logger';

// 1. Define your API using effect-http
const api = Api.make().pipe(
  Api.get('hello', '/hello', {
    response: Api.string(),
  }),
  Api.post('echo', '/echo', {
    request: {
      body: Api.string(),
    },
    response: Api.string(),
  }),
);

// 2. Implement the API handlers
const app = Router.make(api).pipe(
  Router.handle('hello', () => Effect.succeed('Hello from Effect-HTTP!')), 
  Router.handle('echo', ({ body }) => Effect.succeed(`You said: ${body}`)),
);

// 3. Create a Node.js server from the API and application
const server = NodeServer.make(app, { port: 3000 });

// 4. Run the server using NodePlatform.NodeRuntime
pipe(
  server.pipe(
    Effect.tap(() =>
      Effect.logInfo('Server started on http://localhost:3000'),
    ),
  ),
  NodePlatform.NodeRuntime.runMain,
);

view raw JSON →