async function handleRequest(event) {
const { request } = event;
const urlInfo = new URL(request.url);
const proxyRequest = new Request(`https://www.example.com${urlInfo.pathname}${urlInfo.search}`, {
method: request.method,
body: request.body,
headers: request.headers,
copyHeaders: true,
});
proxyRequest.headers.set('Host', 'www.example.com');
const response = await fetch(proxyRequest);
response.headers.append('Access-Control-Allow-Origin', '*');
response.headers.append('Access-Control-Allow-Methods', 'GET,POST');
response.headers.append('Access-Control-Allow-Headers', 'Authorization');
response.headers.append('Access-Control-Max-Age', '86400');
response.headers.delete('X-Cache');
return response;
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event));
});
Was this page helpful?