๐งญ Dynamic & Wildcard Routes in Tirne
This example demonstrates how to use dynamic parameters (e.g., :id
) and wildcard splats (e.g., *
) in your route definitions with tirne
.
Match dynamic paths like/user/:id
or/blog/*
and access the values through theparams
argument.
๐ง Example Code
import { Server, json } from "tirne";
import type { Route } from "tirne";
const routes: Route[] = [
{
method: "GET",
path: "/user/:id",
handler: (req, params) => {
return json({ userId: params?.id });
},
},
{
method: "GET",
path: "/blog/*",
handler: (req, params) => {
return json({ path: params?.["*"] });
},
},
];
const server = new Server(routes);
export default {
fetch: (req: Request) => server.fetch(req),
};
๐งช Functionality Check
Try these curl commands to verify dynamic and wildcard routing:
1. โ
Access /user/123
curl http://localhost:3000/user/123
{ "userId": "123" }
2. โ
Access /blog/2025/06/tech/startup
curl http://localhost:3000/blog/2025/06/tech/startup
{ "path": "2025/06/tech/startup" }