Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

Router

import { Router } from "https://raw.githubusercontent.com/mandarineorg/mandarinets/master/deps.ts";

An interface for registering middleware that will run when certain HTTP methods and paths are requested, as well as provides a way to parameterize parts of the requested path.

class Router<RP extends RouteParams = RouteParams, RS extends State = Record<string, any>> {
constructor(opts?: RouterOptions);
all<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
all<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
all<P extends RouteParams = RP, S extends State = RS>(
nameOrPath: string,
pathOrMiddleware: string | RouterMiddleware<P, S>,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
allowedMethods(options?: RouterAllowedMethodsOptions): Middleware;
delete<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
delete<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
delete<P extends RouteParams = RP, S extends State = RS>(
nameOrPath: string,
pathOrMiddleware: string | RouterMiddleware<P, S>,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
*entries(): IterableIterator<[Route, Route]>;
forEach(callback: (
value1: Route,
value2: Route,
router: this,
) => void
, thisArg?: any): void;
get<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
get<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
get<P extends RouteParams = RP, S extends State = RS>(
nameOrPath: string,
pathOrMiddleware: string | RouterMiddleware<P, S>,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
head<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
head<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
head<P extends RouteParams = RP, S extends State = RS>(
nameOrPath: string,
pathOrMiddleware: string | RouterMiddleware<P, S>,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
*keys(): IterableIterator<Route>;
options<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
options<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
options<P extends RouteParams = RP, S extends State = RS>(
nameOrPath: string,
pathOrMiddleware: string | RouterMiddleware<P, S>,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
param<S extends State = RS>(param: keyof RP, middleware: RouterParamMiddleware<RP, S>): Router<RP, S>;
patch<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
patch<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
patch<P extends RouteParams = RP, S extends State = RS>(
nameOrPath: string,
pathOrMiddleware: string | RouterMiddleware<P, S>,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
post<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
post<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
post<P extends RouteParams = RP, S extends State = RS>(
nameOrPath: string,
pathOrMiddleware: string | RouterMiddleware<P, S>,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
prefix(prefix: string): this;
put<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
put<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
put<P extends RouteParams = RP, S extends State = RS>(
nameOrPath: string,
pathOrMiddleware: string | RouterMiddleware<P, S>,
...middleware: RouterMiddleware<P, S>[],
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
redirect(
source: string,
destination: string,
status?: RedirectStatus,
): this;
routes(): Middleware;
url<P extends RouteParams = RP>(
name: string,
params?: P,
options?: UrlOptions,
): string | undefined;
use<P extends RouteParams = RP, S extends State = RS>(...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
use<P extends RouteParams = RP, S extends State = RS>(path: string | string[], ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
use<P extends RouteParams = RP, S extends State = RS>(pathOrMiddleware: string | string[] | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>;
*values(): IterableIterator<Route<RP, RS>>;
*[Symbol.iterator](): IterableIterator<Route<RP, RS>>;
 
static url(
path: string,
params?: RouteParams,
options?: UrlOptions,
): string;
}

§Type Parameters

§
RP extends RouteParams = RouteParams
[src]
§
RS extends State = Record<string, any>
[src]

§Constructors

§
new Router(opts?: RouterOptions)
[src]

§Methods

§
all<P extends RouteParams = RP, S extends State = RS>(name: string, path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register named middleware for the specified routes when the DELETE, GET, POST, or PUT method is requested.

all<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware for the specified routes when the DELETE, GET, POST, or PUT method is requested.

all<P extends RouteParams = RP, S extends State = RS>(nameOrPath: string, pathOrMiddleware: string | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]
§
allowedMethods(options?: RouterAllowedMethodsOptions): Middleware
[src]

Middleware that handles requests for HTTP methods registered with the router. If none of the routes handle a method, then "not allowed" logic will be used. If a method is supported by some routes, but not the particular matched router, then "not implemented" will be returned.

The middleware will also automatically handle the OPTIONS method, responding with a 200 OK when the Allowed header sent to the allowed methods for a given route.

By default, a "not allowed" request will respond with a 405 Not Allowed and a "not implemented" will respond with a 501 Not Implemented. Setting the option .throw to true will cause the middleware to throw an HTTPError instead of setting the response status. The error can be overridden by providing a .notImplemented or .notAllowed method in the options, of which the value will be returned will be thrown instead of the HTTP error.

§
delete<P extends RouteParams = RP, S extends State = RS>(name: string, path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register named middleware for the specified routes when the DELETE, method is requested.

delete<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware for the specified routes when the DELETE, method is requested.

delete<P extends RouteParams = RP, S extends State = RS>(nameOrPath: string, pathOrMiddleware: string | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]
§
entries(): IterableIterator<[Route, Route]>
[src]

Iterate over the routes currently added to the router. To be compatible with the iterable interfaces, both the key and value are set to the value of the route.

§
forEach(callback: (value1: Route, value2: Route, router: this) => void, thisArg?: any): void
[src]

Iterate over the routes currently added to the router, calling the callback function for each value.

§
get<P extends RouteParams = RP, S extends State = RS>(name: string, path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register named middleware for the specified routes when the GET, method is requested.

get<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware for the specified routes when the GET, method is requested.

get<P extends RouteParams = RP, S extends State = RS>(nameOrPath: string, pathOrMiddleware: string | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]
§
keys(): IterableIterator<Route>
[src]

Iterate over the routes currently added to the router. To be compatible with the iterable interfaces, the key is set to the value of the route.

§
options<P extends RouteParams = RP, S extends State = RS>(name: string, path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register named middleware for the specified routes when the OPTIONS, method is requested.

options<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware for the specified routes when the OPTIONS, method is requested.

options<P extends RouteParams = RP, S extends State = RS>(nameOrPath: string, pathOrMiddleware: string | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]
§
param<S extends State = RS>(param: keyof RP, middleware: RouterParamMiddleware<RP, S>): Router<RP, S>
[src]

Register param middleware, which will be called when the particular param is parsed from the route.

§
patch<P extends RouteParams = RP, S extends State = RS>(name: string, path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register named middleware for the specified routes when the PATCH, method is requested.

patch<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware for the specified routes when the PATCH, method is requested.

patch<P extends RouteParams = RP, S extends State = RS>(nameOrPath: string, pathOrMiddleware: string | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]
§
post<P extends RouteParams = RP, S extends State = RS>(name: string, path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register named middleware for the specified routes when the POST, method is requested.

post<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware for the specified routes when the POST, method is requested.

post<P extends RouteParams = RP, S extends State = RS>(nameOrPath: string, pathOrMiddleware: string | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]
§
prefix(prefix: string): this
[src]

Set the router prefix for this router.

§
put<P extends RouteParams = RP, S extends State = RS>(name: string, path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register named middleware for the specified routes when the PUT method is requested.

put<P extends RouteParams = RP, S extends State = RS>(path: string, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware for the specified routes when the PUT method is requested.

put<P extends RouteParams = RP, S extends State = RS>(nameOrPath: string, pathOrMiddleware: string | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]
§
redirect(source: string, destination: string, status?: RedirectStatus): this
[src]

Register a direction middleware, where when the source path is matched the router will redirect the request to the destination path. A status of 302 Found will be set by default.

The source and destination can be named routes.

§
routes(): Middleware
[src]

Return middleware that will do all the route processing that the router has been configured to handle. Typical usage would be something like this:

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

const app = new Application();
const router = new Router();

// register routes

app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 80 });
§
url<P extends RouteParams = RP>(name: string, params?: P, options?: UrlOptions): string | undefined
[src]

Generate a URL pathname for a named route, interpolating the optional params provided. Also accepts an optional set of options.

§
use<P extends RouteParams = RP, S extends State = RS>(...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware to be used on every matched route.

use<P extends RouteParams = RP, S extends State = RS>(path: string | string[], ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]

Register middleware to be used on every route that matches the supplied path.

use<P extends RouteParams = RP, S extends State = RS>(pathOrMiddleware: string | string[] | RouterMiddleware<P, S>, ...middleware: RouterMiddleware<P, S>[]): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>
[src]
§
values(): IterableIterator<Route<RP, RS>>
[src]

Iterate over the routes currently added to the router.

§
[Symbol.iterator](): IterableIterator<Route<RP, RS>>
[src]

Provide an iterator interface that iterates over the routes registered with the router.

§Static Methods

§
url(path: string, params?: RouteParams, options?: UrlOptions): string
[src]

Generate a URL pathname based on the provided path, interpolating the optional params provided. Also accepts an optional set of options.