Skip to main content
Module

x/acorn/mod.ts>Router

A focused RESTful server framework for Deno 🌰🦕
Go to Latest
class Router
extends EventTarget
import { Router } from "https://deno.land/x/acorn@0.0.3/mod.ts";

A router which is specifically geared for handling RESTful type of requests and providing a straight forward API to respond to them.

A RouteHandler is registered with the router, and when a request matches a route the handler will be invoked. The handler will be provided with Context of the current request. The handler can return a web platform Response instance, BodyInit value, or any other object which will be to be serialized to a JSON string as set as the value of the response body.

The route is specified using the pathname part of the URLPattern API, which supports routes with wildcards (e.g. /posts/*) and named groups (e.g. /books/:id) which are then provided as .params on the context argument to the handler.

When registering a route handler, a Deserializer, Serializer, and ErrorHandler can all be specified. When a deserializer is specified and a request has a body, the deserializer will be used to parse the body. This is designed to make it possible to validate a body or hydrate an object from a request. When a serializer is specified and the handler returns something other than a Response or BodyInit, the serializer will be used to serialize the response from the route handler, if present, otherwise JSON.stringify() will be used to convert the body used in the response.

Observability of the router is designed using DOM events, where there are several events which can be listened for:

  • "error" - produces a RouterErrorEvent when an error occurs when within the router. This can be used to provide a customized response for errors.
  • "listen" - produces a RouterListenEvent when the router has successfully started listening to requests.
  • "handled" - produces a HandledEvent when the router has completed handling of a request and has sent the response.
  • "notfound" - produces a NotFoundEvent when the router was unable to provide a response for a given request. This can be used to provide a customized response for not found events.

Example

import { Router } from "https://deno.land/x/acorn/mod.ts";

const router = new Router();

router.all("/:id", (ctx) => ({ id: ctx.params.id }));

router.listen({ port: 8080 });

Methods

addEventListener(
type: "error",
listener: RouterErrorEventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void
addEventListener(
type: "handled",
listener: HandledEventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void
addEventListener(
type: "listen",
listener: RouterListenEventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void
addEventListener(
type: "notfound",
listener: NotFoundEventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void
all<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(route: R, options: RouteOptionsWithHandler<R, BodyType, Params, ResponseType>): Destroyable
all<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(
route: R,
handler: RouteHandler<ResponseType, BodyType, Params>,
options?: RouteOptions<R, BodyType, Params>,
): Destroyable
delete<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(route: R, options: RouteOptionsWithHandler<R, BodyType, Params, ResponseType>): Destroyable
delete<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(
route: R,
handler: RouteHandler<ResponseType, BodyType, Params>,
options?: RouteOptions<R, BodyType, Params>,
): Destroyable
get<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(route: R, options: RouteOptionsWithHandler<R, BodyType, Params, ResponseType>): Destroyable
get<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(
route: R,
handler: RouteHandler<ResponseType, BodyType, Params>,
options?: RouteOptions<R, BodyType, Params>,
): Destroyable
handle(request: Request): Promise<Response>
head<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(route: R, options: RouteOptionsWithHandler<R, BodyType, Params, ResponseType>): Destroyable
head<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(
route: R,
handler: RouteHandler<ResponseType, BodyType, Params>,
options?: RouteOptions<R, BodyType, Params>,
): Destroyable
listen(options?: ListenOptions): Promise<void>
options<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(route: R, options: RouteOptionsWithHandler<R, BodyType, Params, ResponseType>): Destroyable
options<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(
route: R,
handler: RouteHandler<ResponseType, BodyType, Params>,
options?: RouteOptions<R, BodyType, Params>,
): Destroyable
patch<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(route: R, options: RouteOptionsWithHandler<R, BodyType, Params, ResponseType>): Destroyable
patch<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(
route: R,
handler: RouteHandler<ResponseType, BodyType, Params>,
options?: RouteOptions<R, BodyType, Params>,
): Destroyable
post<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(route: R, options: RouteOptionsWithHandler<R, BodyType, Params, ResponseType>): Destroyable
post<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(
route: R,
handler: RouteHandler<ResponseType, BodyType, Params>,
options?: RouteOptions<R, BodyType, Params>,
): Destroyable
put<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(route: R, options: RouteOptionsWithHandler<R, BodyType, Params, ResponseType>): Destroyable
put<R extends string, Params extends RouteParameters<R>, BodyType, ResponseType>(
route: R,
handler: RouteHandler<ResponseType, BodyType, Params>,
options?: RouteOptions<R, BodyType, Params>,
): Destroyable
[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string)
[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
options: any,
inspect: (value: unknown, options?: unknown) => string,
)