Skip to main content Accessibility Feedback

Cloudflare Worker Function

This boilerplate listens for calls to your Cloudflare Worker endpoint and responds with “Hello World.” It also includes the necessary headers to prevent CORS errors.

Source Code

Example

let response = await fetch('path/to/your/serverless/function');
let data = await response.json();

The Boilerplate

// 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));
});