-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
37 lines (32 loc) · 890 Bytes
/
index.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
const shortLinks = [
{ key: 'git', value: 'https://github.com/matthewjamesboyle' },
{ key: 'linkedin', value: 'https://www.linkedin.com/in/mattjamesboyle/' },
{ key: 'twitter', value: 'https://twitter.com/MattJamesBoyle' },
{ key: 'ukpf', value: 'https://reddit.com/r/ukpersonalfinance' },
]
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond with hello worker text
* @param {Request} request
*/
async function handleRequest(request) {
r = request.url.split('/')
res = matchReq(r[r.length - 1])
if (res === null) {
return new Response('404 :(', {
headers: { 'content-type': 'text/plain' },
})
}
return Response.redirect(res, 301)
}
function matchReq(part) {
for (i = 0; i < shortLinks.length; i++) {
j = shortLinks[i]
if (part === j.key) {
return j.value
}
}
return null
}