Skip to main content

@siglo/express Adapter Guide

For a complete walkthrough, see Serve a REST Endpoint and Watch It Run Live. @siglo/express provides an Express middleware adapter for executing Siglo workflows within traditional Node.js web applications. It automates HTTP request extraction, correlation identifier propagation, cooperative cancellation, timeout enforcement, and semantic error-to-HTTP status code mapping.

1. Overview & Architecture

The Express adapter acts as a route-level or path-level middleware:

2. Installation & Requirements

Install @siglo/express and Siglo:
Peer dependency matrix:
  • Siglo: * (v1.0.0+)
  • express: ^4.18.0 || ^5.0.0

3. API Reference

sigloExpressMiddleware(workflow, options)

[!NOTE] Backward Compatibility: rivuletExpressMiddleware is exported as an identical alias for legacy workflows.
Returns a standard Express middleware function (req, res, next) => Promise<any>.

4. Configuration Options (SigloExpressOptions)

Without a custom output callback, a workflow result with status: 'waiting' returns 202 Accepted with the suspension descriptor as JSON. Completed workflows use the normal 200 OK response. Suspension is a successful result and does not invoke onError. A custom output callback controls both the response body and status. When durable is omitted, Express also accepts X-Durable: true, ?durable=true, or a JSON body containing durable: true. An explicit durable: false overrides these request hints.

5. Built-in Headers & Tracing

The middleware automatically enriches HTTP headers:
  • X-Correlation-Id: Inspects incoming request headers (matching correlationIdHeader or x-request-id). If present, the value is mirrored in the outgoing response header X-Correlation-Id.
  • X-Workflow-Stack-Id: Upon workflow completion or suspension, injects the unique execution stack ID into the response headers. Useful for log correlation and customer support triage.

6. HTTP Error Status Code Mapping

When an unhandled exception is thrown inside a workflow, the adapter’s getErrorStatusCode(err) inspects the error hierarchy and sets appropriate HTTP status codes:

7. Cooperative Request Cancellation

When a client closes an HTTP connection early (e.g. user closes browser or upstream proxy aborts), Express exposes req.signal (in Express 5) or req.raw?.signal. @siglo/express passes this abort signal directly to the workflow engine (ExecutionOptions.signal). Steps inside your workflow can listen to this signal via param.$signal?.():
If the client terminates the connection while the fetch is pending, the HTTP request is cancelled immediately, freeing server sockets.

8. Complete Production Example


9. Durable Workflows and Automatic HTTP Responses

Enable durable: true so waitForEvent() or wait() can suspend execution. The adapter automatically returns 202 Accepted when the result has status: 'waiting', including the suspension descriptor and X-Workflow-Stack-Id header. A workflow that completes uses 200 OK by default.
The waiting JSON includes status, stackId, stepId, correlationKey, eventName, expiresAt (a Unix timestamp in milliseconds when a timeout is set), and continuationStepIndex. Suspension is a successful result, so it does not invoke onError. Calling the submission handler again starts a new execution; approval webhooks must call resumeWorkflow() to continue the suspended one. For request-specific control, replace durable: true with durable: (req) => req.headers['x-durable'] === 'true'. Explicit durable: false disables suspension; wait nodes then use their non-durable behavior. A custom output callback takes full control of the response body and status, including for waiting results. If you supply one, handle result.status === 'waiting' explicitly when you want to preserve the 202 response. The HTTP adapter does not supply persistence. Configure a shared storage adapter that supports checkpoints and wait records to survive process restarts and resume across replicas. Timer waits also need a scheduler to resume them when due. See Durable Workflows, Storage and Adapters, and the distributed checkout tutorial.