Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created November 13, 2023 19:45
Show Gist options
  • Save cferdinandi/180a8580e8ba075e20262f7cc2183f9a to your computer and use it in GitHub Desktop.
Save cferdinandi/180a8580e8ba075e20262f7cc2183f9a to your computer and use it in GitHub Desktop.
// Define response headers
let headers = new Headers({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS, HEAD',
'Access-Control-Allow-Headers': '*'
});
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
// Catchall response
return new Response(JSON.stringify(['Hello, world!']), {
status: 200,
headers: headers
});
}
// Listen for API calls
addEventListener('fetch', function (event) {
event.respondWith(handleRequest(event.request));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment