0.0.49 • Published 8 months ago

@hattip/router v0.0.49

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@hattip/router

Express-style imperative router for Hattip.

Usage

import { createRouter } from "@hattip/router";
import { cookie } from "@hattip/cookie";

const app = createRouter();

// Add middleware
app.use(cookie());

// GET /
app.get("/", () => new Response("Hello, world!"));

// POST /echo
app.post("/echo", async (ctx) => new Response(await ctx.request.text()));

// DELETE /book/:title where :title is a route parameter
app.delete("/book/:title", async (ctx) => {
  // Parameters are available in ctx.params
  return new Response(`Deleted book: ${ctx.params.title}`);
});

export default app.buildHandler();

API

  • createRouter(): Creates a new router.
  • router.<method>(handler): Adds a handler for the given method and all paths.
  • router.<method>(path, handler): Adds a handler for the given method and path.

    path can be a string or a regular expression. If it's a string, parameters can be specified using : followed by the parameter name like /book/:title. * can be used to match any number of characters.

    <method> is the HTTP method in lowercase (e.g. get, post, delete).

    handler is a request handler.

  • router.use(path, handler): Adds a handler for all methods and the given path.

  • router.use(handler): Adds a handler for all methods and all paths.
  • router.handlers: Returns an array of handlers.
  • router.buildHandler(): Returns a handler that can be passed to an adapter.

@hattip/router extends the RequestContext object with a params property which contains a map of parameters extracted from a dynamic route.

0.0.49

8 months ago

0.0.46

12 months ago

0.0.47

10 months ago

0.0.48

9 months ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35-canary.2

2 years ago

0.0.35-canary.7

2 years ago

0.0.35-canary

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.30

2 years ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.21

3 years ago

0.0.22

3 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.20

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.15

3 years ago

0.0.9

3 years ago

0.0.16

3 years ago

0.0.8

3 years ago

0.0.17

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago