Deliver an HTML page from an HTML string directly inside the Worker script.
export default { async fetch(request) { 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", }, }); },};
export default { async fetch(request): Promise<Response> { 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", }, }); },} satisfies ExportedHandler;
from js import Response, Headers def on_fetch(request): html = """<!DOCTYPE html> <body> <h1>Hello World</h1> <p>This markup was generated by a Cloudflare Worker.</p> </body>""" headers = Headers.new({"content-type": "text/html;charset=UTF-8"}.items()) return Response.new(html, headers=headers)
use worker::*; #[event(fetch)]async fn fetch(_req: Request, _env: Env, _ctx: Context) -> Result<Response> { let html = r#"<!DOCTYPE html> <body> <h1>Hello World</h1> <p>This markup was generated by a Cloudflare Worker.</p> </body> "#; Response::from_html(html)}