Taxum / @taxum/core / routing / Router
Class: Router
Defined in: routing/router.ts:57
The Router class is responsible for handling and routing HTTP requests based on specified paths and methods.
It allows nesting of sub-routers, adding layers, defining fallback handlers, and processing requests.
Error Handling
Every route and service in Taxum is allowed to be fallible, as throwing errors in an essential part of the application workflow. Taxum ensures that errors never short-circuit the request-response cycle by wrapping each route and every layered route in a MapErrorToResponse
service.
This means that you can safely throw errors from your routes and services, and Taxum will ensure that they are handled properly. By default, Taxum will convert any error into a 500 Internal Server Error
response, unless they implement the ToHttpResponse
interface, in which case they will be converted to the appropriate response.
If an error results in a 5xx
response, Taxum will log the error. Unless changes, this will use the console
. You can change the error logger by calling setLoggerProxy.
You can also change the error handler to customize the way errors are converted to responses. See Router.errorHandler for more details.
Implements
Constructors
Constructor
new Router():
Router
Returns
Router
Methods
errorHandler()
errorHandler(
errorHandler
):this
Defined in: routing/router.ts:143
Sets the error handler to convert errors into responses.
Parameters
errorHandler
Returns
this
See
fallback()
fallback(
handler
):this
Defined in: routing/router.ts:100
Sets a fallback handler for the service.
The specified handler will be used as a fallback to handle any requests that do not match existing routes.
Parameters
handler
the function to be used as a fallback handler.
Returns
this
invoke()
invoke(
req
):Promise
<HttpResponse
>
Defined in: routing/router.ts:180
Makes an asynchronous HTTP call using the provided request object.
Routes the request to the appropriate handler and returns the response. If no response is returned from the primary handler, it executes the fallback handler.
This function is normally considered internal, but it can be used in unit- and integration testing to avoid spinning up a server.
Parameters
req
the HTTP request object containing all necessary details for the call.
Returns
Implementation of
layer()
layer(
layer
):this
Defined in: routing/router.ts:153
Applies the specified layer to all previously registered routes.
Parameters
layer
the layer to be applied to the endpoints.
Returns
this
methodNotAllowedFallback()
methodNotAllowedFallback(
handler
):this
Defined in: routing/router.ts:133
Adds a fallback handler for the case where a route exists, but the method of the request is not supported.
Sets a fallback on all previously registered MethodRouter
s to be called when no matching method handler is set.
const router = new Router()
.route("/", m.get(helloWorld))
.fallback(handle404)
.methodNotAllowedFallback(handle405);
The fallback only applies if there is a MethodRouter
registered for a given path, but the method used in the request is not specified. In the example, a GET
on /
causes the helloWorld
handler to react, while issuing a POST
triggers handle405
. Calling an entirely different route, like /hello
causes handle404
to run.
Parameters
handler
Returns
this
nest()
nest(
path
,router
):this
Defined in: routing/router.ts:82
Nests a given router under a specific path.
Parameters
path
string
the base path where the nested router will be applied.
router
Router
the router instance to be nested under the specified path.
Returns
this
nestService()
nestService(
path
,service
):this
Defined in: routing/router.ts:87
Parameters
path
string
service
Returns
this
resetFallback()
resetFallback():
this
Defined in: routing/router.ts:108
Resets the fallback handler to the default.
Returns
this
route()
route(
path
,methodRouter
):this
Defined in: routing/router.ts:70
Defines a route for a specified path and associates it with a method router.
Parameters
path
string
the URL path to define the route for.
methodRouter
the router that handles the methods for the given path.
Returns
this
routeLayer()
routeLayer(
layer
):this
Defined in: routing/router.ts:162
Applies the specified layer to all previously registered routes.
Parameters
layer
Returns
this