-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.js
57 lines (51 loc) · 1.52 KB
/
mod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function handleRequest(request) {
const { pathname } = new URL(request.url);
if (pathname.startsWith("/html")) {
const html = `<html>
<p><b>Message:</b> Hello from Deno Deploy.</p>
</html>`;
return new Response(html, {
headers: {
// The interpretation of the body of the response by the client depends
// on the 'content-type' header.
// The "text/html" part implies to the client that the content is HTML
// and the "charset=UTF-8" part implies to the client that the content
// is encoded using UTF-8.
"content-type": "text/html; charset=UTF-8",
},
});
}
if (pathname.startsWith("/json")) {
// Use stringify function to convert javascript object to JSON string.
const json = JSON.stringify({
message: "Hello from Deno Deploy",
});
return new Response(json, {
headers: {
"content-type": "application/json; charset=UTF-8",
},
});
}
return new Response(
`<body
align="center"
style="font-family: Avenir, Helvetica, Arial, sans-serif; font-size: 1.5rem;"
>
<h1>Return JSON and/or HTML Example</h1>
<p>
<a href="/html">/html</a> - responds with HTML to the request.
</p>
<p>
<a href="/json">/json</a> - responds with JSON to the request.
</p>
</body>`,
{
headers: {
"content-type": "text/html; charset=UTF-8",
},
},
);
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});