Skip to Content
HeronJS v4 is now available.
Versionv4HTTPHTTP Configuration

HTTP Configuration

The package remains @heronjs/express and uses Fastify 5 internally. In 4.7.0, public APIs are HttpAdapter and HttpServer; requests, replies, options, hooks and plugins use framework-owned contracts exported by @heronjs/common.

Request and response

Import HttpRequest, HttpResponse, HttpInterceptor, and HttpErrorInterceptor from @heronjs/common. The request exposes id, method, url, originalUrl, headers, cookies, identity and executionSignal. Body, params and query are unknown until validated. It does not expose a native raw request.

Responses support code/status, header, getHeader, send, sent and statusCode. Session access uses request.session?.get(key), set({key, value}) and delete(key). Parameter decorators remain available from @heronjs/express.

Ordinary handlers accept values, Promises and Observables and send the first emission. Manual replies avoid a second send. Observe request.executionSignal to cancel work on disconnect or shutdown. Use SSE for streaming.

Typed lifecycle hooks

import type {HttpOptions} from '@heronjs/common'; const options: HttpOptions = { bodyLimit: 1024 * 1024, trustProxy: false, hooks: { beforeHandler: [({request, response}) => { response.header({name: 'x-request-id', value: request.id}); }], }, cors: {origin: 'https://example.test'}, securityHeaders: true, }; await app.listen({port: 3000, options});

hooks is an object of arrays: onRequest, beforeHandler, afterResponse, onReady, and onClose. Framework plugins receive a context with addHooks(hooks). interceptors and globalError use framework request/response contracts. Express middleware and next() are unsupported.

Native engine customization

Use setup for native plugins, parsers and engine-specific integrations. A disposable lease borrows the application engine without closing it:

import {http} from '@heronjs/express'; import type {HttpOptions} from '@heronjs/common'; const options: HttpOptions = { setup() { using engine = http(); engine.value.addHook('onRequest', async (request, reply) => { reply.header('x-native-request-id', request.id); }); }, };

http() and useHttp() are also installed globally at application startup. setup runs before readiness. Keep the engine application-owned; do not close it when releasing the lease. See resource hooks for multiple applications and disposal rules.

The previous FastifyHttpOptions, FastifyInterceptor, FastifyErrorInterceptor, FastifyAdapter and FastifyServer names are removed. Top-level fastify, helmet, session, contentTypeParsers, parsers, preset and integrations options are rejected. Move native configuration to setup; use top-level bodyLimit, trustProxy, cors, and securityHeaders for framework options.

Shared listener and shutdown

REST, GraphQL, tRPC, WebSocket, MCP and ACP share the application listener. Protocol request contexts use framework contracts; obtain native capabilities through their feature hooks. HTTP remains the default transport; runtime: {stdio: true} enables stdio for an MCP or ACP endpoint that declares transport: 'stdio'.

Default proxy trust is false, the body limit is 1 MiB, and the bind address is 0.0.0.0 on port 3000. Shutdown aborts owned request execution and forces remaining HTTP connections closed after shutdownTimeout (default 5000 ms). Native plugins must complete their own lifecycle hooks.

Server configuration

Use protocols for protocol settings and options for HTTP settings passed to app.listen(). The subServer alias is removed in 4.3.0, and unsupported top-level server settings are rejected. Configure endpoint authentication and transport on the relevant MCP, ACP or WebSocket decorator.

Last updated on