Skip to main content

@ditsmod/routing

The @ditsmod/routing module implements a router with the Router interface:

interface Router {
on(method: HttpMethod, path: string, handle: RouteHandler): this;

all(path: string, handle: RouteHandler): this;

find(method: HttpMethod, path: string): RouterReturns;
}

type RouteHandler = (
nodeReq: NodeRequest,
nodeRes: NodeResponse,
params: PathParam[],
queryString: any
) => Promise<void>;

class RouterReturns {
handle: RouteHandler | null;
params: PathParam[] | null;
}

interface PathParam {
key: string;
value: string;
}

A ready-made example of using this module can be found in any example in the Ditsmod repository.

Installation and importing

Installation:

npm i @ditsmod/routing

Importing:

import { rootModule } from '@ditsmod/core';
import { RoutingModule } from '@ditsmod/routing';

@rootModule({
imports: [RoutingModule],
// ..
})
export class AppModule {}

Custom router integration

If you want to integrate a custom router for the Ditsmod application, it is enough for your router to implement the above Router interface, after which it can be added to the providers at the application level:

import { featureModule, Router } from '@ditsmod/core';

import { MyRouter } from './my-router.js';

@featureModule({
providersPerApp: [{ token: Router, useClass: MyRouter }],
})
export class MyCustomRouterModule {}

Extensions group ROUTES_EXTENSIONS

The extensions group token ROUTES_EXTENSIONS is exported from this module. Extensions from this group return metadata with interface Array<MetadataPerMod2> is an array of dynamic metadata that is intended for creating request handlers. Each item of this array is a separate route.

Extensions group PRE_ROUTER_EXTENSIONS

The extensions group token PRE_ROUTER_EXTENSIONS is also exported from this module. An extension from this group uses the metadata returned by the ROUTES_EXTENSIONS extension group to create HTTP request handlers.