import { Router } from "https://deno.land/x/acorn@0.0.1/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
Example
import { Router } from "https://deno.land/x/acorn/mod.ts";
const router = new Router();
router.all("/:id", (ctx) => {
return { id: ctx.params.id };
});
router.listen({ port: 8080 });
Methods
Add a handler for a route associated with DELETE
, GET
, POST
, and
PUT
requests. The returned value is a handle which can be used to
unregister the handler.