Last active
November 14, 2021 10:49
-
-
Save wemakefuture/8ed79085e69a238b55419126e9241654 to your computer and use it in GitHub Desktop.
Reverse Proxy Cloudflare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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