Skip to main content

http-router

logo

HTTP router for standard Request and Response.

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM


Packages

The package supports multiple platforms.

  • deno.land/x - https://deno.land/x/http_router/mod.ts
  • npm - @httpland/http-router

What

Provides a router for routing HTTP requests. The essence of an HTTP router is a function that receives a Request and returns a Response. Also, routing always consists of declarative conditions and branching.

Declarative conditions

The first-class conditions that this project addresses are HTTP request method and URL path. The result is an API similar to those used by many libraries.

import { Router } from "https://deno.land/x/http_router@$VERSION/mod.ts";

const router = new Router();
router.get("/greet", () => new Response("hello"));

Mapping conditions to handlers using methods corresponding to HTTP methods and URL paths conforming to the URLPattern API.

Router

Router is a router that can match HTTP request methods and HTTP request URL paths.

Since the Web convention is to use HTTP method and URL path to identify resources, this should satisfy most people’s needs.

It registers a handler from the method corresponding to the HTTP request method, and can also use the dynamic paths available in the URLPattern API.

import { Router } from "https://deno.land/x/http_router@$VERSION/mod.ts";
import { serve } from "https://deno.land/std@$VERSION/http/mod.ts";
import logger from "https://deno.land/x/http_log@$VERSION/mod.ts";
import cors from "https://deno.land/x/http_cors@$VERSION/mod.ts";

const router = new Router();

router
  .all(logger())
  .all("/api/*", cors())
  .get(
    "/api/users",
    (request) =>
      Response.json([{ id: "0", name: "Alice" }, { id: "1", name: "Bob" }]),
  );

serve(router.handler);

API

All APIs can be found in the deno doc.

Benchmark

Benchmark script with comparison to several popular routers is available.

deno task bench

Benchmark results can be found here.

More detailed references:

License

Copyright © 2023-present httpland.

Released under the MIT license