Skip to content

Instantly share code, notes, and snippets.

@ffeii
Last active September 15, 2019 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ffeii/9981c91ab3183cd6640c9410896a4dc5 to your computer and use it in GitHub Desktop.
Save ffeii/9981c91ab3183cd6640c9410896a4dc5 to your computer and use it in GitHub Desktop.
fetch-through-cloudflare-workers
addEventListener('fetch', event => {
let request = event.request
if (request.method !== 'GET') {
event.respondWith(new Response('Only for GET method'))
return
}
let url = request.url.replace(new URL(request.url).origin, '')
if (url.startsWith('/https://') || url.startsWith('/http://')) {
url = url.replace('/', '')
event.respondWith(fetch(url))
} else {
const body = `<!DOCTYPE html>
<html lang="en" style="height: 80vh;display: flex;justify-content: center;align-items: center;">
<meta name="viewport" content="width=device-width, initial-scale=1">
<input id="i" type="text">
<button onclick=location.href="/"+document.getElementById("i").value>GET</button>
</html>`
event.respondWith(new Response(body, {headers: new Headers({"Content-Type": "text/html"})}))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment