2.1.0 • Published 1 year ago

@httpland/http-router v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

http-router

deno land deno doc npm GitHub

HTTP request router for standard Request and Response.

  • URL pattern matching
  • Tiny, lean

HTTP router

Create HTTP request router with URL pattern and method handler map.

import { createRouter } from "https://deno.land/x/http_router@$VERSION/mod.ts";
import { serve } from "https://deno.land/std@$VERSION/http/mod.ts";
const router = createRouter({
  "/api/students/:name": {
    GET: (req, params) => {
      const greeting = `Hello! ${params.name!}`;
      return new Response(greeting);
    },
  },
  "/api/status": () => new Response("OK"), // Any HTTP request method
});
await serve(router);

HEAD request handler

By default, if a GET request handler is defined, a HEAD request handler is automatically added.

This feature is based on RFC 9110, 9.1

All general-purpose servers MUST support the methods GET and HEAD.

import { createRouter } from "https://deno.land/x/http_router@$VERSION/mod.ts";
import { serve } from "https://deno.land/std@$VERSION/http/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
const router = createRouter({
  "/": {
    GET: (req) => {
      const body = `Hello! world`;
      return new Response(body, {
        headers: {
          "content-length": new Blob([body]).size.toString(),
        },
      });
    },
  },
});

const req = new Request("http://localhost", { method: "HEAD" });
const res = await router(req);
assertEquals(res.body, null);
assertEquals(res.headers.get("content-length"), "12");

This can be disabled by setting withHead to false.

import { createRouter } from "https://deno.land/x/http_router@$VERSION/mod.ts";
createRouter({}, { withHead: false });

Performance

Benchmark script with comparison to several popular routers is available. Run it with deno bench --unstable.

License

Copyright © 2022-present httpland.

Released under the MIT license

3.0.0-beta.3

1 year ago

3.0.0-beta.2

1 year ago

3.0.0-beta.5

1 year ago

3.0.0-beta.4

1 year ago

3.0.0-beta.7

1 year ago

3.0.0-beta.6

1 year ago

2.1.0-beta.1

2 years ago

2.1.0-beta.3

2 years ago

2.1.0-beta.2

2 years ago

3.0.0-beta.1

2 years ago

1.0.0-beta.6

2 years ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

2.0.0-beta.3

2 years ago

1.2.0

2 years ago

1.2.0-beta.1

2 years ago

1.2.0-beta.3

2 years ago

1.2.0-beta.2

2 years ago

1.2.0-beta.5

2 years ago

1.2.0-beta.4

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

1.0.0-beta.5

2 years ago

1.0.0-beta.4

2 years ago

1.0.0-beta.3

2 years ago