router-segments

router-segments

Router with segments

This router take full advantages of segments, building a tree of regexp to avoid having 500 regexp calls for one route.

router-segments uses path-to-regexp for matching the route paths, also used by expressjs, koa-router and many other routers.

import { createRouterBuilder } from "router-segments";

const builder = createRouterBuilder();

builder.add("/", ref);
builder.addSegment("/post", (segmentBuilder) => {
segmentBuilder.add("/:id(\\d+)-:slug([A-Za-z\\-]+)", ref, "/post/view");
segmentBuilder.defaultRoute(ref, "/post");
});

export default builder.createRouter();

path-to-regexp documentation

router.toPath("/post/view", { id: "001", slug: "a-slug" });
// /post/001-a-slug
import { createRouterBuilder } from "router-segments";

const builder = createRouterBuilder(["en", "fr"]);

builder.add("/", ref);
builder.addLocalizedSegment(
{
en: "/my-blog",
fr: "/mon-blog",
},
(segmentBuilder) => {
segmentBuilder.addLocalized(
{
en: "/post/:id(\\d+)",
fr: "/billet/:id(\\d+)",
},
ref,
"/my-blog/post",
);
segmentBuilder.defaultRoute(ref, "/my-blog");
},
);

export const router = builder.createRouter();

see Definition file