From 5d9b7b08f25bc1379bfc2ee65e67baaa7ea2d99b Mon Sep 17 00:00:00 2001 From: kj415j45 <919815238@qq.com> Date: Wed, 6 Sep 2023 09:05:32 +0800 Subject: [PATCH] Allow CORS. --- src/worker.ts | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/worker.ts b/src/worker.ts index f3393a2..86f6398 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -5,28 +5,34 @@ import log from './routes/log'; export default { async fetch(request: any, env: Env, ctx: ExecutionContext): Promise { - try { - const reqPath = new URL(request.url).pathname; - - // dynamic route - if (reqPath.startsWith('/log/') && reqPath.length > 1) { - return log(request, env, ctx); - } - - // static route - switch (reqPath) { - case '': - case '/': - return index(request, env, ctx); - default: - return error(request, env, ctx); - } - } catch (e) { - return error(request, env, ctx); - } + const res = await handle(request, env, ctx); + res.headers.set('Access-Control-Allow-Origin', '*'); + return res; }, async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) { ctx.waitUntil(cleanup(env)); }, }; + +async function handle(request: any, env: Env, ctx: ExecutionContext): Promise { + try { + const reqPath = new URL(request.url).pathname; + + // dynamic route + if (reqPath.startsWith('/log/') && reqPath.length > 1) { + return log(request, env, ctx); + } + + // static route + switch (reqPath) { + case '': + case '/': + return index(request, env, ctx); + default: + return error(request, env, ctx); + } + } catch (e) { + return error(request, env, ctx); + } +}