Handlers
Handlers answer requests. defineCoreHandler() and defineErrorHandler() create them, and the adapters wrap handlers written for Node.js or the Fetch API. The Handlers guide explains return values and middleware.
Factories
defineCoreHandler
declare function defineCoreHandler(input: Omit<CoreHandlerOptions, 'type'>): Handler;
declare function defineCoreHandler(input: CoreHandler): Handler;
type CoreHandler = (event: IAppEvent) => unknown | Promise<unknown>;Creates a handler that runs while no error is pending. It takes the function alone, or options with the function as fn.
app.get('/users/:id', defineCoreHandler((event) => {
return { id: event.params.id };
}));
app.use(defineCoreHandler({
path: '/health',
method: 'GET',
timeout: 500,
fn: () => 'ok',
}));defineErrorHandler
declare function defineErrorHandler(input: Omit<ErrorHandlerOptions, 'type'>): Handler;
declare function defineErrorHandler(input: ErrorHandler): Handler;
type ErrorHandler = (error: AppError, event: IAppEvent) => unknown | Promise<unknown>;Creates a handler that runs only while an error is pending and receives it as an AppError. It handles errors of the handlers registered before it; see Error Handling.
app.use(defineErrorHandler((error, event) => {
event.response.status = error.status;
return { message: error.message };
}));Options
HandlerBaseOptions
type HandlerBaseOptions = {
method?: Uppercase<MethodName> | Lowercase<MethodName>,
path?: Path,
timeout?: number,
onBefore?: HandlerBeforeListener,
onAfter?: HandlerAfterListener,
onError?: HandlerErrorListener,
};The options both factories accept next to fn.
| Name | Type | Default | Meaning |
|---|---|---|---|
method | MethodName, upper or lower case | none: every method | Method the handler answers; GET also answers HEAD |
path | Path | none | Path appended to the path the handler is registered under; see Paths |
timeout | number | the App's handlerTimeout | Milliseconds for fn; see Timeouts |
onBefore | HandlerBeforeListener: (event) => unknown | none | Runs before fn |
onAfter | HandlerAfterListener: (event, response) => unknown | none | Runs after the return value of fn became a Response |
onError | HandlerErrorListener: (error, event) => unknown | none | Runs when fn, onBefore or onAfter throws |
- A method shortcut (
app.get(), ...) replacesmethod. - With a
method, the handler matches its path exactly. Without one, it matches every path that starts with its path, as middleware does. - The callbacks are explained in Lifecycle Callbacks. Their return values are ignored.
CoreHandlerOptions
type CoreHandlerOptions = HandlerBaseOptions & {
type: typeof HandlerType.CORE,
fn: CoreHandler,
};The options of a core handler. defineCoreHandler() sets type.
ErrorHandlerOptions
type ErrorHandlerOptions = HandlerBaseOptions & {
type: typeof HandlerType.ERROR,
fn: ErrorHandler,
};The options of an error handler. defineErrorHandler() sets type.
Adapters
fromNodeHandler
declare function fromNodeHandler(handler: NodeHandler): Handler;
type NodeHandler = (req: IncomingMessage, res: ServerResponse) => unknown | Promise<unknown>;Wraps a Node.js (req, res) handler. It ends the pipeline: the handlers after it do not run.
app.use('/legacy', fromNodeHandler((req, res) => {
res.end('Hello from Node.js');
}));- It needs a Node.js server that exposes
reqandres; underapp.fetch()with a plainRequestthe request answers 500. See Express Compatibility. - Passing something other than a function throws an
AppErrorwhen the handler is created.
fromNodeMiddleware
declare function fromNodeMiddleware(handler: NodeMiddleware): Handler;
type NodeMiddleware = (
req: IncomingMessage,
res: ServerResponse,
next: (err?: unknown) => void,
) => unknown | Promise<unknown>;Wraps a Node.js (req, res, next) middleware, such as one written for Express.
import cors from 'cors';
app.use(fromNodeMiddleware(cors()));next()continues with the next routup handler, unless the middleware already ended the response.next(error)makeserrorthe pending error.- The same Node.js server requirement as
fromNodeHandler()applies.
fromWebHandler
experimentaldeclare function fromWebHandler(input: WebHandler): Handler;
declare function fromWebHandler(input: WebHandlerProvider): Handler;
interface WebHandler {
(request: Request): Response | Promise<Response>;
}
interface WebHandlerProvider {
fetch(request: Request): Response | Promise<Response>;
}Wraps a function or object that answers a Fetch API Request, such as another framework's app.
app.use('/proxy', fromWebHandler((request) => fetch(request)));- It passes
event.requeston unchanged. The URL keeps the mount path: a handler mounted at/proxysees/proxy/..., not/.... - Its return value is the response; the handlers after it do not run.
- Passing something other than a function or an object with a
fetchmethod throws anAppErrorwhen the handler is created.