@openknot-ts/knot-rest-http-node
v0.0.6-preview
Published
Serves OpenKnot REST routes on the native Node HTTP host.
Readme
@openknot-ts/knot-rest-http-node
Serves @openknot-ts/knot-rest routes on the native Node host of @openknot-ts/knot-http-node. The router and the selected route sets join through its requirements.
Usage
import { http } from "@openknot-ts/knot-http-node";
import { restNodeHttp } from "@openknot-ts/knot-rest-http-node";
import { defineApplication } from "@openknot-ts/wire";
export const application = defineApplication({ modules: [behavior, flow, io, transport, http, restNodeHttp] });API
| Export | Description |
| --- | --- |
| restNodeHttp | Ready module: implements the host's httpHandler. |
| nodeRestHandler({ requirements?, maxBodySize? }) | The same with a body limit from the application's own config, which replaces REST_MAX_BODY_SIZE. |
Behavior
- Works on
IncomingMessageandServerResponsedirectly. - Reads a body only for routes that take one: requires
application/jsonand enforcesREST_MAX_BODY_SIZEfromcontent-lengthand while streaming. 413 and 415 close the connection, because the rest of the body stays unread. - The request's signal aborts when the response closes early, so a client that goes away cancels its execution and rolls its work back. A stopping host still answers what it admitted.
Custom configuration
const handler = nodeRestHandler({
requirements: { settings },
maxBodySize: ({ settings }) => settings.API_BODY_LIMIT,
});
const server = nodeHttpServer({ handler, listen: ({ settings }) => ({ port: settings.API_PORT }) });
// modules: [..., providePlugs([server])] instead of http and restNodeHttpA custom handler does not bind the shared httpHandler, so several native servers can use their own handlers and limits.
Example and tests
pnpm --filter @openknot-ts/knot-rest-http-node exampletest/http.test.ts runs on a real host: status codes, limits, a client disconnect that leaves no published or retained work, and a request admitted before stop that is still answered.
