HttpProtect.jsonMiddleware() method

A middleware that rejects with a JSON error body.

Signature:

jsonMiddleware(): (req: Request, res: Response, next: NextFunction) => Promise<void>;

Returns:

(req: Request, res: Response, next: NextFunction) => Promise<void>

An async middleware to hand to app.use().

Remarks

The usual choice for an API. The body is {"error":{"type":"HTTP","code":429,"message":"Too Many Requests"}}, with 418 and I'm a teapot for a ban. Content-Type is set as described in HttpProtect.textMiddleware().

Example

const protect = new HttpProtect({ redis: existingClient });

app.use(protect.jsonMiddleware());

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.