Skip to content

Event ​

Every handler receives an event: the request, its route parameters, the response under construction and the continuation to the next handler. The Request guide and the Response guide show it in use.

IAppEvent ​

typescript
interface IAppEvent {
    readonly request: AppRequest;
    readonly params: Record<string, string | undefined>;
    readonly path: string;
    readonly method: MethodNameLike;
    readonly mountPath: string;
    readonly route?: AppEventRoute;
    readonly headers: Headers;
    readonly searchParams: URLSearchParams;
    readonly response: AppResponse;
    readonly store: Record<string | symbol, unknown>;
    readonly appOptions: Readonly<AppOptions>;
    readonly signal: AbortSignal;
    next(error?: Error): Promise<Response | undefined>;
    readonly nextCalled: boolean;
    readonly nextResult: Promise<Response | undefined> | undefined;
    whenNextCalled(): Promise<void>;
}

The type of the event argument of every handler.

PropertyTypeMeaning
requestAppRequestThe request
paramsRecord<string, string | undefined>Route parameters; undefined for an optional parameter that did not match
pathstringPath of the request URL without the query string, also inside a mounted App
methodMethodNameLikeRequest method
mountPathstringThe part of path the running handler's route matched; / for a route without a path
routeAppEventRoute | undefinedThe last method-bound route that ran in this request
headersHeadersRequest headers
searchParamsURLSearchParamsQuery parameters, parsed on first read
responseAppResponseStatus and headers of the response under construction
storeRecord<string | symbol, unknown>Per-request data shared between handlers
appOptionsReadonly<AppOptions>Options of the App answering the request; see AppOptions
signalAbortSignalAborts on a timeout or when the request is aborted; see Timeouts
next(error?)Promise<Response | undefined>Run the handlers after this one
nextCalledbooleanWhether next() was called on this event
nextResultPromise<Response | undefined> | undefinedThe promise of the first next() call
whenNextCalled()Promise<void>Resolves once next() is called
  • Each handler gets its own event. store, response and route belong to the request and are shared by all of them. params, mountPath and signal can differ per handler.
  • route is undefined until a method-bound route ran: in a middleware before its event.next(), and for a request that only middleware handled.

event.next() ​

typescript
next(error?: Error): Promise<Response | undefined>;

Runs the handlers matched after this one and resolves with their response, or undefined when none answered.

typescript
app.use(defineCoreHandler(async (event) => {
    const start = Date.now();
    const response = await event.next();

    console.log(`${event.path} ${response?.status} ${Date.now() - start}ms`);

    return response;
}));
  • Only the first call runs the handlers. Every later call returns the same promise.
  • A handler that returns undefined after calling next() forwards its result. One that returns undefined without calling it waits until it does or event.signal aborts; see Timeouts.
  • next(error) makes error the pending error, so only error handlers run after it; see Error Handling.
  • It does not reject when a later handler throws. It resolves with the response of the error handler that answered, or undefined.

AppRequest ​

typescript
type AppRequest = ServerRequest;

The request type of srvx: a Web Request with fields the server adapter fills in.

FieldTypeMeaning
ipstring | undefinedClient address as the runtime reports it; getRequestIP() also reads forwarding headers
runtimeServerRuntimeContext | undefinedRuntime name and native objects, such as runtime.node.req and runtime.node.res on Node.js
contextServerRequestContext | undefinedArbitrary context of the request
waitUntil((promise: Promise<unknown>) => void | Promise<void>) | undefinedKeep the runtime alive until promise settles

A plain Request passed to app.fetch() has none of these fields.

AppResponse ​

typescript
type AppResponse = {
    status: number;
    headers: Headers;
};

The status and headers of event.response. They apply when a handler returns a plain value (string, object, null, ...), not when it returns a Response.

FieldTypeDefaultMeaning
statusnumber200Status of the response
headersHeadersemptyHeaders of the response
typescript
app.post('/users', defineCoreHandler((event) => {
    event.response.status = 201;
    event.response.headers.set('location', '/users/1');

    return { id: 1 };
}));

AppEventRoute ​

typescript
type AppEventRoute = {
    readonly path: string;
    readonly pathPrefix: string;
    readonly method: string;
};

The value of event.route.

FieldTypeMeaning
pathstringThe route's full pattern over every mount, such as /api/users/:id
pathPrefixstringThe part of path from the App's path option and the mount paths, / without any
methodstringThe method the route is bound to; GET also when it serves a HEAD request

See Dispatched Route.