I'm trying to get familiar with CloudFlare workers.
I've copied a quickstart project from https://developers.cloudflare.com/workers/get-started/quickstarts, via:
wrangler generate my-app https://github.com/cloudflare/worker-template
This gives me a JS file that listens for "fetch" and returns a Response via handleRequest:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
return new Response('Hello worker!', {
headers: { 'content-type': 'text/plain' },
})
}
Ok, that makes sense.
Now I want to include one of Cloudflare's examples
export default {
fetch() {
const html = `<!DOCTYPE html>
<body>
<h1>Hello World</h1>
<p>This markup was generated by a Cloudflare Worker.</p>
</body>
`;
return new Response(html, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
});
},
};
This makes less sense. Am I supposed to replace all of the original code with that? If I do, I get an error on wrangler publish:
Error: Something went wrong with the request to Cloudflare... No event handlers were registered. This script does nothing.
Am I supposed to mash them together somehow? How? Is there a step or configuration I'm missing?