createRequestHandler
Create Node/Express middleware for SSR documents and loader/action HTTP requests.
Compatibility
Import from @kasperrt/smolter/utils/http
Signature
function createRequestHandler({
build,
entry,
dataEndpoint,
}: RequestHandlerOptions): (
incoming: ServerIncomingMessage,
outgoing: ServerResponse,
next: NextHandler,
) => Promise<void>;Options
build exports default(request, entry) and handleDataRequest(request), each returning a Response or Promise<Response>. entry is the deployed browser entry URL from the Vite manifest. See the Express and Node hosting guide for a complete server.
import { createRequestHandler } from "@kasperrt/smolter/utils/http";
app.use(createRequestHandler({ build, entry: "/assets/client.js" }));Middleware order
Mount Prometheus, tracing, authentication, body size limits and static serving first. Do not consume the request stream with a body parser before this middleware. Genuine render and stream failures are passed to next(error). Clean client disconnects cancel the response without calling error middleware. Express trust proxy remains your application’s responsibility. The adapter does not start a server, install Prometheus, or depend on Express.
Returns
Route data uses the reserved .__smolter suffix. /users.__smolter?page=2 dispatches /users?page=2; /users/.__smolter preserves the trailing slash. Document requests call build.default. Explicit dataEndpoint retains legacy ?url= transport. Invalid targets return 400. Data responses use private, no-store. Apply authorization to both representations; reserve the suffix in Express/static middleware. Client disconnection aborts the Request; responses stream with backpressure.
Early shell interactivity
Configure earlyHydration on renderApplication in your server entry, not on this middleware. Keep reverse-proxy buffering disabled if the shell must arrive before optional data. The adapter respects response backpressure and flushes compression when the response provides flush(). A disconnected browser aborts the request and cancels its response reader. Responses returned after disconnection are cancelled instead of piped to a closed socket.