added another middleware example
This commit is contained in:
24
middleware.ts
Normal file
24
middleware.ts
Normal 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 });
|
||||||
Reference in New Issue
Block a user