-
Notifications
You must be signed in to change notification settings - Fork 0
/
l.js
53 lines (43 loc) · 1.14 KB
/
l.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
const html404 = `<!DOCTYPE html>
<body>
<h1>404 Not Found.</h1>
<p>The url you attempted to visit does not exist.</p>
</body>`;
let response_header = {
'content-type': 'text/html;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST'
};
let json_response_header = {
'content-type': 'text/json;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST'
};
async function handleRequest(request) {
if (request.method === 'OPTIONS') {
return new Response(``, {
headers: response_header
});
}
const requestURL = new URL(request.url);
const casePath = requestURL.pathname;
// if (casePath === "/robots.txt") {
// return new Response("User-agent: *\nDisallow: /");
// }
const path = casePath.toLowerCase();
// console.log(path);
const dest = await LINKS.get(path);
if (dest) {
return Response.redirect(dest);
}
// If request not in kv, return 404
return new Response(html404, {
headers: {
'content-type': 'text/html;charset=UTF-8'
},
status: 404
});
}
addEventListener('fetch', async (event) => {
event.respondWith(handleRequest(event.request));
});