Skip to main content
Module

x/acorn/router.ts>Router#on

A focused RESTful server framework for Deno 🌰🦕
Go to Latest
method Router.prototype.on
import { Router } from "https://deno.land/x/acorn@0.4.0/router.ts";

Allows setting a handler that will be called when a response has been handled, but before any default handling has occurred and before it has been sent back to the client.

When the router is ready to form a response and send it to the client, it will match the status of any handlers, and call them in the order they were registered. The status can either be a Status, or a StatusRange or an array of codes and ranges.

The handler will receive the current Context along with the Status code and any Response object that was created previously. If there is no response, none has yet been created.

A handler can return a response body, like a route handler can or BodyInit or a Response and that will then become that basis for a new response. If the handler returns undefined the existing response or the default router response will be used.

Handlers can be removed by calling .destroy() on the returned handle.

If you are performing logging or metrics on the router, it is better to use the event listener interfaces for the "handled" event, as the intention of .on() is to provide a way to do "post-processing" of response or provide custom responses for things like 404 or 500 status responses.

Parameters

status: S | S[]
handler: StatusHandler<S>

Allows setting a handler that will be called when a response has been handled, but before any default handling has occurred and before it has been sent back to the client.

When the router is ready to form a response and send it to the client, it will match the status of any handlers, and call them in the order they were registered. The status can either be a Status, or a StatusRange or an array of codes and ranges.

The handler will receive the current Context along with the Status code and any Response object that was created previously. If there is no response, none has yet been created.

A handler can return a response body, like a route handler can or BodyInit or a Response and that will then become that basis for a new response. If the handler returns undefined the existing response or the default router response will be used.

Handlers can be removed by calling .destroy() on the returned handle.

If you are performing logging or metrics on the router, it is better to use the event listener interfaces for the "handled" event, as the intention of .on() is to provide a way to do "post-processing" of response or provide custom responses for things like 404 or 500 status responses.