Skip to content

HttpRouter.onResponse ​

app.onResponse(hook) registers a named synchronous callback for every response served by that router. Register it in server setup before app.listen(...), not inside a route.

Signature ​

tyrus
app.onResponse(hook: (access: HttpAccessLog) => void): void

The callback runs once after the server finishes producing the response body or discards it. This includes a StreamBody response. It observes the final status and duration but cannot alter the response, and it is not a route preHandler.

tyrus
import { createRouter, HttpAccessLog, HttpError, HttpRouter } from "tyrus:http";
import { Duration } from "tyrus:temporal";

function recordAccess(access: HttpAccessLog): void {
    const elapsed: Duration = access.duration;
    console.log(access.requestId, access.status);
    console.log("assert:", access.status);
}

const app: HttpRouter = createRouter();
app.onResponse(recordAccess);

try {
    app.listen(3000);
} catch (error: HttpError) {
    console.error(error.code);
}

Registering more than one hook is allowed. Hooks run in registration order. The callback may perform any permitted synchronous application work, such as updating a metric, writing an audit record, or forwarding data to a tracing adapter.

error

tyrus
import { createRouter, HttpAccessLog, HttpRouter } from "tyrus:http";

const app: HttpRouter = createRouter();
app.onResponse((access: HttpAccessLog): void => {}); 

The hook must be a named function. A named callback gives the server a stable native entry point and prevents a closure from outliving its captured route state.