-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (31 loc) · 944 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
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
// Define allowed domain origins
// TODO: Replace these with environment variables
const allowed = [
'https://newprojectstarterkit.com',
'https://pshry.com'
]
// Define HTTP headers
const headers = new Headers({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
'Access-Control-Allow-Headers': '*'
})
// If domain is not allowed, return error code
if (!allowed.includes(request.headers.get('origin'))) {
return new Response('Not allowed', { status: 403, headers: headers });
}
// Define response
const response = {
greeting: 'hello world'
}
// Return response
return new Response(JSON.stringify(response), { status: 200, headers: headers })
}