url-short-cfw/index.js

36 lines
893 B
JavaScript
Raw Permalink Normal View History

2020-10-24 07:00:18 +00:00
const urls = require('./urls.json')
2020-10-24 07:49:56 +00:00
addEventListener('fetch', event => {
2020-10-24 07:00:18 +00:00
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const path = new URL(request.url).pathname.substring(1)
console.log(request.headers['user-agent'], new Date(), path)
if (path in urls)
return new Response(null, {
status: 308,
headers: { location: urls[path] },
})
2020-10-24 07:49:56 +00:00
else
return new Response(
2020-10-27 05:37:58 +00:00
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
2021-06-18 16:06:57 +00:00
<title>My URL Shortner</title>
2020-10-27 05:37:58 +00:00
</head>
<body>
404 not found !! Check all possible at
2021-06-18 12:33:25 +00:00
urls.json
2020-10-27 05:37:58 +00:00
</body>
</html>
`,
2020-10-27 05:01:29 +00:00
{
status: 404,
2020-10-27 05:37:58 +00:00
headers: { 'content-type': 'text/html' },
2020-10-27 05:01:29 +00:00
},
2020-10-24 07:49:56 +00:00
)
2020-10-24 07:00:18 +00:00
}