JavaScript Examples - Return HTML

Generate a complete HTML page from a string and return it directly from a function. Use this pattern to serve lightweight pages, landing screens, or maintenance notices on Azion’s global network without rendering them on an origin server.

const html = `<!DOCTYPE html>
<body>
<h1>Hello World</h1>
<p>This markup was generated by Azion - Functions.</p>
</body>`
async function handleRequest(request) {
return new Response(html, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest(event.request))
})

How it works

The markup is stored in a template literal assigned to the html constant, so the whole page lives inside the function. When the fetch event fires, handleRequest builds a new Response with that string as the body and sets the content-type header to text/html;charset=UTF-8, which tells the browser to render the payload as a web page rather than plain text. event.respondWith() then delivers the page, close to the user and without a round trip to an origin.