HTML example

This is an example of how to return an HTML response.

You can also preview it at https://andreterron-htmlExample.web.val.run?name=Andre

Readme
1
2
3
4
5
6
7
8
9
10
11
12
// View at https://andreterron-htmlExample.web.val.run?name=Andre
export default async function(req: Request): Promise<Response> {
const query = new URL(req.url).searchParams;
// Read name from the querystring or body. Defaults to "you" if not present.
const name = query.get("name") || (await req.json().catch(() => ({}))).name || "you";
// Returns the HTML response
return new Response(`<h1>Hi ${name}!</h1>`, {
headers: { "Content-Type": "text/html" },
});
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.