addEventListener("fetch", event => event.respondWith(handleRequest(event)));
async function handleRequest(event) {
const req = event.request;
console.log('Request headers', Object.fromEntries(req.headers));
const reqBody = ["GET", "HEAD"].includes(req.method) ? undefined : await req.text();
console.log('Request body', reqBody)
const beReq = new Request(req.url, {
method: req.method,
headers: req.headers,
body: reqBody,
})
const beResp = await fetch(beReq, { backend: "origin_0" });
console.log('Response headers:', Object.fromEntries(beResp.headers));
const beRespBody = await beResp.text();
console.log('Response body:', beRespBody);
return new Response(beRespBody, {
status: beResp.status,
headers: beResp.headers
});
}