Input type of route handlers and chained middleware
Output type of route handlers and chained middleware
Chains an error handler to the router
Error handler to chain
A new router with the given error handler chained and the existing routes
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.
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")
})
Chains a middleware to the router
Input type of the next
function of the middleware
Output type of the next
function of the middleware
Middeware to chain
A new router with the given middleware chained and the existing routes
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.
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 })
Generated using TypeDoc
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
Example: Chaining middleware to a route handler