From 779eeed56e5f6a62c1c28253973ebaa172bbd82f Mon Sep 17 00:00:00 2001 From: Vincent Lannurien Date: Wed, 28 Jan 2026 22:03:24 +0100 Subject: [PATCH] added another middleware example --- middleware.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 middleware.ts diff --git a/middleware.ts b/middleware.ts new file mode 100644 index 0000000..6ae92c7 --- /dev/null +++ b/middleware.ts @@ -0,0 +1,24 @@ +import { Application, Context, Next, Router } from "jsr:@oak/oak"; + +interface HelloWorldState { + hello?: string; + world?: string; +} +async function hello(ctx: Context, next: Next) { + ctx.state.hello = "Hello"; + await next(); +} +async function world(ctx: Context, next: Next) { + ctx.state.world = "world"; + await next(); +} + +const router = new Router(); +router.get("/hello", hello, world, (ctx) => { + ctx.response.body = `${ctx.state.hello}, ${ctx.state.world}!`; +}); + +const app = new Application(); +app.use(router.routes()); +app.use(router.allowedMethods()); +app.listen({ port: 8000 });