Toruk

Deno std http router. Simple, close to the platform.

Getting Started

Path uses URLPattern matching.

import { serve } from 'https://deno.land/std/http/server.ts';
import { httpRouter } from 'https://deno.land/x/toruk/mod.ts';

const router = httpRouter({
  routes: [
    {
      path: '/',
      handler: () => new Response('Hello World'),
      children: [
        {
          path: 'users/:id',
          handler: ({ params }) => new Response(`User ${params.id}`),
        },
      ],
    },
  ],
});

serve(router);

Alternative Syntaxes

Express

import { serve } from 'https://deno.land/std/http/server.ts';
import { httpRouter } from 'https://deno.land/x/toruk/mod.ts';

const router = new Router()
  .get('/', (req) => {
    return new Response('Hello World');
  })
  .post<{ id: string }>('/users/:id', ({ params }) => {
    return new Response('User: ' + params.id);
  });

serve(router.toHandler());

Export/Import workflow

index.ts

import { httpRouter } from 'https://deno.land/x/toruk/mod.ts';
import * as users from './routes/users/:id.ts'

const router = httpRouter({
  routes: [
    users
  ]
});

routes/users/:id.ts

import { RouteHandler } from 'https://deno.land/x/toruk/mod.ts';

export const path = '/users/:id';

export const handler: RouteHandler<{ id: string }> = ({ params }) => {
  return new Response(`User ${params.id}`);
};