Where other documents use req, they use the name req instead of _req https://github.com/denoland/fresh/blob/b7281792a0a5b2674f659514b1e58116fabd6460/docs/latest/concepts/middleware.md?plain=1#L153
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { FreshContext, MiddlewareHandler } from "$fresh/server.ts";
|
|
|
|
// cors middleware
|
|
async function corsHandler(req: Request, ctx: FreshContext) {
|
|
if (req.method == "OPTIONS") {
|
|
return new Response(null, {
|
|
status: 204,
|
|
});
|
|
}
|
|
const origin = req.headers.get("Origin") || "*";
|
|
const resp = await ctx.next();
|
|
const headers = resp.headers;
|
|
|
|
headers.set("Access-Control-Allow-Origin", origin);
|
|
headers.set("Access-Control-Allow-Credentials", "true");
|
|
headers.set(
|
|
"Access-Control-Allow-Headers",
|
|
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With",
|
|
);
|
|
headers.set(
|
|
"Access-Control-Allow-Methods",
|
|
"POST, OPTIONS, GET, PUT, DELETE",
|
|
);
|
|
|
|
return resp;
|
|
}
|
|
|
|
// log middleware
|
|
async function logHandler(_req: Request, ctx: FreshContext) {
|
|
const since = new Date();
|
|
const resp = await ctx.next();
|
|
const latency = (+new Date()) - (+since);
|
|
resp.headers.set("latency", `${latency}`);
|
|
return resp;
|
|
}
|
|
|
|
async function rootHandler(_req: Request, ctx: FreshContext) {
|
|
ctx.state.root = "root_mw";
|
|
const resp = await ctx.next();
|
|
resp.headers.set("server", "fresh test server");
|
|
return resp;
|
|
}
|
|
|
|
async function kindHandler(_req: Request, ctx: FreshContext) {
|
|
const resp = await ctx.next();
|
|
resp.headers.set("destination", ctx.destination);
|
|
return resp;
|
|
}
|
|
|
|
export const handler: MiddlewareHandler | MiddlewareHandler[] = [
|
|
rootHandler,
|
|
logHandler,
|
|
kindHandler,
|
|
corsHandler,
|
|
];
|