Skip to content

Instantly share code, notes, and snippets.

@VishalTaj
Last active May 27, 2022 05:51
Show Gist options
  • Save VishalTaj/9ae12db1756447e4c5ae3533bd396fe0 to your computer and use it in GitHub Desktop.
Save VishalTaj/9ae12db1756447e4c5ae3533bd396fe0 to your computer and use it in GitHub Desktop.
Cloudflare Worker to rewrite the response
async function handleRequest(req) {
const res = await fetch(req)
if (res.status == 404) {
let init = {...res};
init.status = 200;
init.statusText = "OK";
init.webSocket = init.webSocket || undefined;
let response = await (new Response(res.body, init))
console.log('overwrite status', response.status)
return response
}
return res
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment