Return HTML
Python example
Returning HTML via a Cloudflare Worker.
def handleRequest(request):
imgSrc="https://images.pexels.com/photos/1033729/pexels-photo-1033729.jpeg"
# defines a html string for the JSON content
html="""
<!DOCTYPE html>
<html>
<head>
<script>
function msg() {
document.getElementById("content").innerHTML = "Hello World.";
}
</script>
</head>
<body>
<h1>Hello World.</h1>
<p>This markup was generated by a Cloudflare Worker.</p>
<img src="{}" height="20px" width="20px"/>
<p>
<form>
<input type="button" value="Click me" onclick="msg()">
</form>
</p>
<div id="content">
</div>
</body>
</html>
""".format(imgSrc)
# a new Response
return __new__(Response(html, {
'headers' : { 'content-type' : 'text/html;charset=UTF-8'}
}))
# call the Worker runtime
addEventListener('fetch', (lambda event: event.respondWith(handleRequest(event.request))))
Which returns the following page.
Credits
Photo by Asad Photo Maldives from Pexels