Node.js Runtime
NodeRuntimeAdapter bridges Web Request/Response handling to Node's HTTP server APIs.
Basic Usage
import { serve } from "@hornjs/fest";
import { NodeRuntimeAdapter } from "@hornjs/fest/node";
serve({
adapter: new NodeRuntimeAdapter(),
fetch() {
return new Response("Hello from Node");
},
});
Constructor Options
new NodeRuntimeAdapter(options) accepts NodeServerOptions, which combines:
http.ServerOptionshttps.ServerOptionshttp2.ServerOptionsnet.ListenOptionshttp2?: boolean
That means you can pass both listener options and runtime-specific Node server options directly to the adapter constructor.
Commonly Used Options
http2: switch the adapter tonode:http2.createSecureServer()requestTimeout: configure the HTTP server request timeoutkeepAliveTimeout: configure keep-alive timeoutmaxHeaderSize: limit accepted header sizeallowHTTP1: relevant when HTTP/2 is enabled and you want HTTP/1 fallbackipv6Only,backlog,exclusive,signal: inherited fromnet.ListenOptions
Official References
- Node HTTP server options: https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener
- Node HTTPS server options: https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener
- Node listen options: https://nodejs.org/api/net.html#serverlistenoptions-callback
- Node HTTP/2 secure server options: https://nodejs.org/api/http2.html#http2createsecureserveroptions-onrequesthandler
What the Adapter Does
For each incoming request, the adapter:
receives IncomingMessage and ServerResponse
converts the request into a standard Request
runs it through server.fetch()
writes the resulting Response back to Node's response object
HTTPS and HTTP/2
If TLS options are present, the adapter creates an HTTPS server by default.
When http2: true is passed to NodeRuntimeAdapter, it creates a secure
HTTP/2 server instead.
import { NodeRuntimeAdapter } from "@hornjs/fest/node";
const adapter = new NodeRuntimeAdapter({ http2: true });
HTTP/2 requires TLS configuration.
Close Behavior
server.close(true) asks the Node adapter to close active HTTP connections when
the underlying server supports closeAllConnections().
TypeScript Entrypoints
For local examples, Node.js can run .ts files with:
node --experimental-strip-types server.ts
If you need JSX or TSX, use the CLI with a loader:
JITI_JSX=1 fest --entry ./server.tsx --import jiti/register