- 3.0.0-beta.7Latest
- 3.0.0-beta.6
- 3.0.0-beta.5
- 3.0.0-beta.4
- 3.0.0-beta.3
- 3.0.0-beta.2
- 3.0.0-beta.1
- 2.1.0
- 2.1.0-beta.3
- 2.1.0-beta.2
- 2.1.0-beta.1
- 1.0.0-beta.6
- 2.0.0
- 2.0.0-beta.3
- 2.0.0-beta.2
- 2.0.0-beta.1
- 1.2.0
- 1.2.0-beta.5
- 1.2.0-beta.4
- 1.2.0-beta.3
- 1.2.0-beta.2
- 1.2.0-beta.1
- 1.1.0
- 1.1.0-beta.1
- 1.0.0
- 1.0.0-beta.5
- 1.0.0-beta.4
- 1.0.0-beta.3
- 1.0.0-beta.2
- 1.0.0-beta.1
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.
Related
More detailed references:
License
Copyright © 2023-present httpland.
Released under the MIT license