Class Router<I, O>

A router is a middleware that routes requests to other middlewares based on their method and path

Remarks

Routes can contain named parameters which instead of matching part of the path verbatim will capture one or many path segments and make them available by name to the route handler. The provided path parameters is a properly typed object with the same keys as the named parameters.

:name will capture a single path segment up to the next / or end of the path while *name will capture any number of path segments until the end of the path.

The router provides facilities to chain middleware both to the entire router through the use method and to individual routes through a Transform parameter.

This router is based on a prefix tree which means the order in which routes are registered does not influence the matching process and the number of routes does not impact performance.

Priority is given to verbatim segments over single named parameters (:name) over multiple named parameters (*name). This means that /hello/there would be matched before /hello/:name which would be matched before /hello/*name.

Routes are matched case insensitively. Of course captured path segments are not modified and their case is preserved.

Example: Simple route handler

const app = router()
app.get("/hello/:name", ({ path }) => response.text(`Hello ${path.name} !`))

Example: Chaining middleware to a route handler

const app = router()
app.post(
"/search/:term",
(ctx) => ctx.use(middleware),

(ctx) => {
// ctx.path.term is the string captured by the :term parameter
// ctx will also contain any value the middleware might have added
// and will by typed accordingly
}
)

Type Parameters

Implements

Methods

Chaining

Routing

Methods

Chaining

  • Chains an error handler to the router

    Parameters

    Returns Router<I, O>

    A new router with the given error handler chained and the existing routes

    Remarks

    Note that this method behaves the same as use and returns a new router with shared routes. It will only catch errors thrown by the routes registered on the new router and by middleware chained after it.

    The error handler returns the same type as route handlers and chained middleware, since its goal is to gracefully handle errors and return an appropriate response. It is good practice to re-throw errors that the error handler doesn't know how to produce a response for so that upstream error handlers can do it instead.

    Example: Catching errors

    let app = router()

    // errors thrown from this middleware won't be caught
    app = app.use(middelware1)
    // errors thrown from this route won't be caught
    app.get("/foo", () => {
    throw new Error("foo")
    })

    app = app.catch((error, input) => {
    console.error(error)
    return response.status(500)
    })

    // errors thrown from this middleware will be caught
    app = app.use(middleware2)
    // errors thrown from this route will be caught
    app.get("/bar", () => {
    throw new Error("bar")
    })

    See

    Chain.catch

  • Chains a middleware to the router

    Type Parameters

    • II extends RouterContext

      Input type of the next function of the middleware

    • OO

      Output type of the next function of the middleware

    Parameters

    Returns Router<II, OO>

    A new router with the given middleware chained and the existing routes

    Remarks

    Note that this method returns a new router. The new router shares the same routes as the original one, but the middleware will not apply to the previously registered routes, only the ones registered on the new router.

    Since the routes are shared, this method can be called two times on the same router and routes can be registered on the two resulting routers, and all three routers will share all the registered routes.

    Example: Sharing routes

    const app = router()
    const app1 = app1.use(middleware1)
    const app2 = app2.use(middleware2)

    app.get("/foo", () => response.text("foo"))
    app1.get("/bar", () => response.text("bar"))
    app2.get("/baz", () => response.text("baz"))

    // /foo, /bar and /baz are all available
    serve(app, { port: 8080 })

    See

    Chain.use

Routing

  • Registers the given DELETE route handler with its own middleware chain

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    • II

      Transformed route input passed to the handler

    • OO

      Transformed route output returned by the handler

    Parameters

    Returns this

  • Registers the given DELETE route handler

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    Parameters

    Returns this

  • Registers the given GET route handler with its own middleware chain

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    • II

      Transformed route input passed to the handler

    • OO

      Transformed route output returned by the handler

    Parameters

    Returns this

  • Registers the given GET route handler

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    Parameters

    Returns this

  • Registers the given PATCH route handler with its own middleware chain

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    • II

      Transformed route input passed to the handler

    • OO

      Transformed route output returned by the handler

    Parameters

    Returns this

  • Registers the given PATCH route handler

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    Parameters

    Returns this

  • Registers the given POST route handler with its own middleware chain

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    • II

      Transformed route input passed to the handler

    • OO

      Transformed route output returned by the handler

    Parameters

    Returns this

  • Registers the given POST route handler

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    Parameters

    Returns this

  • Registers the given PUT route handler with its own middleware chain

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    • II

      Transformed route input passed to the handler

    • OO

      Transformed route output returned by the handler

    Parameters

    Returns this

  • Registers the given PUT route handler

    Type Parameters

    • const Route extends `/${string}`

      String literal type representing the route

    Parameters

    Returns this

Generated using TypeDoc