Created
March 1, 2018 09:43
-
-
Save anonymous/f734510ce0a356b04cfce013aa005f9d to your computer and use it in GitHub Desktop.
Testing a Cloudflare worker's fetch(). We want to run fetch() in the background without having to wait for a response, so we can respond immediately to the initial request.
This file contains 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
addEventListener('fetch', event => { | |
event.respondWith(fetchAndFetch(event.request)) | |
}) | |
async function fetchAndFetch(request) { | |
const content = `timestamp=${Date.now()}` | |
// Actually a POST request. | |
console.log(content) | |
// Respond. | |
return new Response(`Sent a POST request with content: ${content} \n`) | |
} | |
// Monkey-patch console.log to fetch(). | |
console.log = function() { | |
fetch('https://putsreq.com/ZyjM4ubsAADQyGBDRJ08', { | |
method: 'POST', | |
body: JSON.stringify([...arguments]) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment