Router
The Router class is NOT part of the public Corpus API. It is automatically created by the Server module and each Route, StaticRoute, WebSocketRoute, Controller, and Middleware object will register itself into the global router. Even though it is not part of the public API, if you really need to, you can still access the global instance by importing $routerStore and calling the .get() method.
Contents
Router Adapters
When instantiating a new Server, you may optionally provide a supported adapter inside the ServerOptions.
BranchAdapter (default)
The default router adapter, based on @medley/router by nwoltman (Nathan Woltman). This is an extremely fast radix-tree router. The original library is CJS only so i had to fork it for this package. (Attributions included.)
MemoiristAdapter
An alternative adapter layer for memoirist by SaltyAom. This is the router used in ElysiaJS. Requires the optional memoirist dependency. Also extremely fast.
import { C, MemoiristAdapter } from "@ozanarslan/corpus";
const server = new C.Server({
adapter: new MemoiristAdapter(),
});
Creating Your Own Adapter
You can implement a custom router adapter by satisfying the RouterAdapterInterface. The interface and supporting types are exported by name from the package.
import { C } from "@ozanarslan/corpus";
import type {
RouterAdapterInterface,
RouterReturnData,
RouterRouteData,
} from "@ozanarslan/corpus";
class MyAdapter implements RouterAdapterInterface {
add(data: RouterRouteData): void {
// register a route
}
find(req: C.Request): RouterReturnData | null {
// return matched route data, or null if not found
// You can also throw in here and it will be handled by
// the Server.handleError method.
}
// optionally return all registered routes
list: () => Array<RouterRouteData> | undefined;
}
const server = new C.Server({
adapter: new MyAdapter(),
});