1
0

return cors headers for 404 response as well

This commit is contained in:
2024-09-28 02:19:07 +02:00
parent fd7dce7256
commit 0e78ec4787

View File

@@ -406,21 +406,23 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
} }
const route = this.getRoute(req) const route = this.getRoute(req)
let response: Response | undefined
if (!route) if (!route)
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 }) response = new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
const handlers = [...this.globalHandlers, ...route.handlers] if (!response) {
const dbg = this.debugMode const handlers = [
...this.globalHandlers,
let response: Response | undefined ...(route?.handlers ?? [])
]
for (const handler of handlers) { for (const handler of handlers) {
const context = { const context = {
...(ctxExt ?? {}), ...(ctxExt ?? {}),
env, env,
req, req,
dbg, dbg: this.debugMode,
ctx ctx
} as RouterContext<Env, CtxExt, ReqExt> } as RouterContext<Env, CtxExt, ReqExt>
@@ -431,9 +433,10 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
break break
} }
} }
}
if (!response) if (!response)
return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 }) response = new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
if (this.corsEnabled) { if (this.corsEnabled) {
response = new Response(response.body, response) response = new Response(response.body, response)