Middleware (@shadow-js/dusk/server)
Middleware: runs around the rest of the request.
Middleware: runs around the rest of the request. Return a Response to answer without calling
next, or call next() and return (or replace) its response. Calling next twice is an error.
next() always settles with a Response, also when the rest of the request fails: an error
thrown by a route, an HttpError, a redirect(), a validation failure or an error of an inner
middleware arrives as the response the client would get (a 500 with the reason phrase, the
HttpError's status and headers, the 3xx). Headers set after await next() are therefore on
error responses too. To let a failure take another shape, return a different response or throw
your own error (an HttpError is mapped like any other). Requests the guards or CSRF check
reject never reach middleware.
Import#
import type { Middleware } from "@shadow-js/dusk/server";
Signature#
type Middleware = (event: RequestEvent, next: Next) => Response | Promise<Response>
Examples#
const timing: Middleware = async (event, next) => {
const started = performance.now();
const response = await next();
response.headers.set("server-timing", `app;dur=${performance.now() - started}`);
return response;
};