Skip to content

Instantly share code, notes, and snippets.

@wemakefuture
Last active November 14, 2021 10:49
Show Gist options
  • Save wemakefuture/8ed79085e69a238b55419126e9241654 to your computer and use it in GitHub Desktop.
Save wemakefuture/8ed79085e69a238b55419126e9241654 to your computer and use it in GitHub Desktop.
Reverse Proxy Cloudflare
const DOMAIN = 'hook.wemakefuture.com'; // path to be proxied
const ORIGIN = 'hook.integromat.com'; // where to fetch content from
addEventListener('fetch', event => {
var url = new URL(event.request.url);
if (url.pathname != "") {
console.log(url.pathname)
event.respondWith(handleRequest(event, url))
}
})
async function handleRequest(event, url) {
const text = await event.request.clone().text()
// Change URL from public URL to use the origin URL
var originUrl = url.toString().replace(
'https://' + DOMAIN + url.pathname,
'https://' + ORIGIN + url.pathname
);
try {
const json = JSON.parse(text)
//console.log("json")
return await fetch(originUrl, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(json)
});
} catch(e) {
//console.log("text")
return await fetch(originUrl, {
method: 'POST',
body: text
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment