added another middleware example

This commit is contained in:
2026-01-28 22:03:24 +01:00
parent e72776ad54
commit 779eeed56e

24
middleware.ts Normal file
View File

@@ -0,0 +1,24 @@
import { Application, Context, Next, Router } from "jsr:@oak/oak";
interface HelloWorldState {
hello?: string;
world?: string;
}
async function hello(ctx: Context<HelloWorldState>, next: Next) {
ctx.state.hello = "Hello";
await next();
}
async function world(ctx: Context<HelloWorldState>, next: Next) {
ctx.state.world = "world";
await next();
}
const router = new Router<HelloWorldState>();
router.get("/hello", hello, world, (ctx) => {
ctx.response.body = `${ctx.state.hello}, ${ctx.state.world}!`;
});
const app = new Application<HelloWorldState>();
app.use(router.routes());
app.use(router.allowedMethods());
app.listen({ port: 8000 });