This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmod.js
More file actions
73 lines (66 loc) · 2.16 KB
/
Copy pathmod.js
File metadata and controls
73 lines (66 loc) · 2.16 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Every request to a Deno Deploy program is considered as a fetch event.
// So let's register our listener that will respond with the result of
// our request handler on "fetch" events.
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
if (request.method !== "POST") {
return new Response(null, {
status: 405,
statusText: "Method Not Allowed",
});
}
// We want the 'content-type' header to be present to be able to determine
// the type of data sent by the client. So we respond to the client with
// "Bad Request" status if the header is not available on the request.
if (!request.headers.has("content-type")) {
return new Response(
JSON.stringify({ error: "please provide 'content-type' header" }),
{
status: 400,
statusText: "Bad Request",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
},
);
}
const contentType = request.headers.get("content-type");
const responseInit = {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
};
// Handle JSON data.
if (contentType.includes("application/json")) {
const json = await request.json();
return new Response(JSON.stringify({ json }, null, 2), responseInit);
}
// Handle form data.
if (
contentType.includes("application/x-www-form-urlencoded") ||
contentType.includes("multipart/form-data")
) {
const formData = await request.formData();
const formDataJSON = {};
for (const [key, value] of formData.entries()) {
formDataJSON[key] = value;
}
return new Response(
JSON.stringify({ form: formDataJSON }, null, 2),
responseInit,
);
}
// Handle plain text.
if (contentType.includes("text/plain")) {
const text = await request.text();
return new Response(JSON.stringify({ text }, null, 2), responseInit);
}
// Reaching here implies that we don't support the provided content-type
// of the request so we reflect that back to the client.
return new Response(null, {
status: 415,
statusText: "Unsupported Media Type",
});
}