[]previous and next page

createHandler

Creates the request handler: the single fetch(Request) => Response contract that adapters, the dev server, the test client and prerendering all call.

Creates the request handler: the single fetch(Request) => Response contract that adapters, the dev server, the test client and prerendering all call.

The core pipeline is: guards (URL length, host, framing, body size, timeout) -> route resolution -> CSRF -> middleware -> the endpoint (API route, server function, page, stream, image, draft or nav gate) -> secure headers. The endpoints are features: a handler has only the ones in features, and a request for an endpoint the handler lacks is a 404. Configuration errors (a bad option, a conflicting route, an invalid environment, a manifest or option that needs a missing feature) throw here, at startup.

Import#

ts
import { createHandler } from "@shadow-js/dusk/server";

Signature#

ts
function createHandler(options: HandlerOptions): DuskHandler

Parameters#

Name Description
options The manifest, the features and the settings. See HandlerOptions.

Returns#

The handler.

Throws#

  • TypeError / RangeError for invalid options or a missing feature; EnvError for an invalid environment.

Examples#

ts
import { pages } from the "@shadow-js/dusk/features/pages" subpath;
import { rpc } from the "@shadow-js/dusk/features/rpc" subpath;
const handler = createHandler({
  manifest,
  features: [pages(), rpc()],
  security: { allowedHosts: ["example.com"], cookieSecrets: [process.env.COOKIE_SECRET] },
});
const response = await handler.fetch(new Request("https://example.com/"));