Skip to main content

🦕 VelociRouter

A minimal async RequestResponse router powered by URL Pattern API magic ✨

const router = new Router();

router.use((request, response) => {
  console.log(`[${request.method}] ${request.url}`);
  return response;
});

Native JavaScript URL Pattern matching for routes.

router.get({pathname: '/api/hello/:name'}, (request, response, {match}) => {
  const {name} = match.pathname.groups;
  return new Response(`Hello ${name}!`);
});

Request & Response are forwarded through all matching routes in order.

router.all('/api/*', (request, response) => {
  if (response) {
    response.headers.set('x-api-version', '1');
  }
  return response;
});

Documentation coming…

Deno Server

The handle() method forwards a Request via all matching routes in the order they were created. It will resolve to a Response.

const router = new Router<Deno.ServeHandlerInfo>();

router.get({pathname: '/ip'}, (request, response, {platform}) => {
  return new Response(platform?.remoteAddr.hostname);
});

Deno.serve(
  (request, info) => router.handle(request, info);
);

Notes

Only Deno and Chromium based browsers have URL Pattern API support right now. Other runtimes like Bun and Node require a polyfill.

Inspired by Polka.


MIT License | Copyright © 2023 David Bushell