What Are Cloudflare Workers?

3-minute read
cloudflareworkersserverless

During my free time, I explore the services that I am currently using. One of them is Cloudflare, which I use to manage my domain names. Cloudflare has a lot of services, and one of them is Workers.

I want to share what I know about Workers from a high-level architecture perspective. So, what are Workers?

Cloudflare Workers, or simply Workers, are small pieces of code written in JavaScript and uploaded by users to execute custom logic directly on Cloudflare’s edge servers. They run on the V8 JavaScript engine and do not include Node.js APIs.

Workers can be a good option when you have lightweight backend logic or small-scale projects. They offer lower latency and reduced maintenance overhead compared with self-hosted solutions.

Cloudflare provides a generous free tier of 100,000 requests per day. The main downside for me is that Workers only support JavaScript, TypeScript, and WebAssembly.

Example of a Worker

export default {
	async fetch(request, env, ctx): Promise<Response> {
		return new Response("Hello World!");
	},
} satisfies ExportedHandler<Env>;

From this example, a Worker is a function that takes a request and returns a response. When I visit a Worker’s URL, the fetch function runs and returns the response.

The interesting parts here are env and ctx:

  • env holds your bindings: databases, storage, secrets, and more.
  • ctx lets you do work after the response is sent with ctx.waitUntil().

Create a Project

npm create cloudflare@latest my-worker

It will give you a list of options to choose from. Pick the “Hello World” Worker when it asks. It sets up the project, installs Wrangler—Cloudflare’s CLI—and creates a wrangler.jsonc configuration file.

Then move to the folder and run it locally:

cd my-worker
npx wrangler dev

This starts a local server. Open it and you will see your message. You can edit the code, save it, and it reloads.

When you are happy with it, deploy it with:

npx wrangler deploy

Wrangler uploads your Worker and gives you a URL like my-worker.your-name.workers.dev. That is it—your code is running worldwide.

Just pay attention to the cost once traffic grows.

Summary

A Worker is just a function, and remember env. The power comes from bindings: attach a database, a storage bucket, or a queue, and they show up in that env argument.