xory.dev

Cloudflare Workers

TIL

Cloudflare allows you to create & deploy serverless scripts for free (with some limitations).

These scripts are called Cloudflare Workers. They run on v8 engine so you can use javascript or language that supports web assembly.

Here are some examples from Cloudflare.

Of course, you must be aware of one of the biggest limitations is 10 ms CPU time, which means you won’t be able to do any heaver processing.

But other than that you could use Cloudflare Key Value service ( Key-Value database ). To have a simple backend with a database. Though just as with workers in the free tier you only get 1000 writes

addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
const value = await NAMESPACE.get("TODO_LIST");
if (value === null) {
return new Response("Value not found", { status: 404 });
}

return new Response(value);
}

Above is an example code of Cloudflare worker that on HTTP request queries Cloudflare KV store and returns data.