Module

x/servest/router.ts>Router

🌾A progressive http server for Deno🌾
Latest
interface Router
implements Route
import { type Router } from "https://dotland.deno.dev/x/servest@v1.3.4/router.ts";

Methods

use(...handlers: ServeHandler[]): void

Set global middleware. It will be called for each request on any routes.

handle(pattern: string | RegExp, ...handlers: RouteHandler[]): void

Register route with given pattern. It will be called for every http method, Examples: router.handle("/", ...) => Called if request path exactly matches "/". router.handle(/^//, ...) => Called if request path matches given regexp.

route(prefix: string, ...handlers: (RouteHandler | Router)[]): void

Register route with given prefixer. This is similar to router.handle() but different in several points:

  • Only string prefix can be passed.
  • Handlers will be called if req.path STARTS WITH prefix
  • route() doesn't designate a single route handler set before dispatching. This means it will keep calling all handler sets that matches prefixer until someone responds.
  • route can accept Router Examples router.route("/users", ...) => Called if request path STARTS WITH "/users".
get(pattern: string | RegExp, ...handlers: RouteHandler[]): void

Register GET/HEAD route. This is shortcut for handle();

post(pattern: string | RegExp, ...handlers: RouteHandler[]): void

Register POST route. This is shortcut for handle()

put(pattern: string | RegExp, ...handlers: RouteHandler[]): void

Register PUT route

delete(pattern: string | RegExp, ...handlers: RouteHandler[]): void

Register DELETE route

options(pattern: string | RegExp, ...handlers: RouteHandler[]): void

Register OPTIONS route

ws(pattern: string | RegExp, handler: WebSocketHandler): void

Accept ws upgrade

ws(
pattern: string | RegExp,
handlers: RouteHandler[],
): void
catch(handler: ErrorHandler): void

Set global error handler. All unhandled promise rejections occured on processing requests will be passed . Only one handler can be set for one router.

finally(handler: ServeHandler): void

Set global finalizer. Every request will reach this handler. Note that request may already has been responded by other handlers. Only one handler can be set for one router.