-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathworker.js
More file actions
28 lines (26 loc) · 1 KB
/
worker.js
File metadata and controls
28 lines (26 loc) · 1 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
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// Get the unique ID from the request URL
// This commit is compleatly working fine with minimal fewatures
const url = new URL(request.url);
const id = url.searchParams.get("id");
// Get the list of URLs from the raw text file
const urlList = await fetch('https://raw.githubusercontent.com/developeranaz/Serverless-URL-Shortner/main/urls.txt')
.then(response => response.text())
.then(text => text.split('\n'))
.catch(error => {
return new Response(error, { status: 500 })
})
// Find the URL associated with the unique ID
const redirectURL = urlList.find(u => u.split(" ")[0] === id);
if (!redirectURL) {
return new Response('Invalid ID', { status: 404 });
}
// Redirect to the associated URL
return new Response('', {
status: 301,
headers: { 'Location': redirectURL.split(" ")[1] }
});
}