rhythmjs

Search documentation

Search guides, the tutorial and every package.

On this page

RhythmRouter#

new RhythmRouter({ prefix? })#
Create an HTTP controller. A router does not extend Rhythm; it compiles routes and middleware down to a single middleware. The prefix joins onto every route path registered on this instance.
get / post / put / patch / delete(path, ...handlers)#
Add one or more handlers for a method and path; multiple handlers compose left to right, and a leading derive middleware types the context for the handlers after it. Matched parameters are in ctx.params.
use(fn)#
Add a middleware. It takes only functions; mount a nested router as use(child.middleware()); the mount is opaque, so the child carries its own full prefix. Registration order is execution order: a middleware wraps only the routes registered after it. There is no startup context or register(); startup values live on the host Rhythm app.
middleware()#
Compile this router to a plain middleware: the one form that mounts anywhere, into a Rhythm app or into another router. The middleware is tagged with the router as its source, so a parent module lists it in its sources.
entries#
A read-only snapshot of registered middlewares and routes, in order.
joinPath(prefix, path)#
The exported prefix-joining helper the router uses internally.
ctx.json(data, status?)#
Serialize data as the body with an application/json content type; status is optional.
ctx.text(body, status?) / ctx.html(body, status?)#
Set a plain-text or HTML body with the matching content type.
ctx.error(status, message?)#
Set the status and a plain-text message, defaulting the message from the status code.
ctx.redirect(url, status?)#
Set the location header with a 302 default and no body.

The helpers are sugar over ctx.response, built into the context by createHttpContext. Context exports (RhythmHttpContext, RhythmResponse, RhythmResponseBody, createHttpContext, and toResponse) live at @rhythmjs/router/context; @rhythmjs/router/adapters/context remains as a compatibility alias for the same module.

Serving exports#

toFetchHandler(app)#
From @rhythmjs/router/fetch: takes a Rhythm app and returns (request: Request) => Promise<Response>, the raw primitive for composing and testing without a listener.
errorToResponse(error)#
From @rhythmjs/router/fetch: maps a thrown error to a Response: the error's own status/statusCode when set, else a logged, masked 500. Call it in your own try/catch around the fetch handler.
RhythmRouterOptions / RhythmRouterContext#
Root type exports describing constructor options and matched path parameters.

Subpath exports: @rhythmjs/router, ./context, and ./fetch, plus the ./adapters/context compatibility alias. The package is coupled to Bun on purpose; there are no runtime adapters.