Building a backend API with workers


I feel I should have managed with the docs but I have been unable to implement the scenario I have in mind so I turn here and see if someone can help me.

I want try out building a backend API on top of workers. For example to support a mobile application or similar.

I’m a bit worried that as the functionality grows I might not be able to fit everything in one worker which has a standard limitation of 1 MB.

I have an idea of how to design this (might not be the optimal one, please feel free point me in other directions if you like but I also want to try this one out).
Here is what I want to achieve.

goes to worker1 where I have internal routing with Hono/Itty, similar.

goes to worker2 where I have internal routing with Hono/Itty, similar.

On top of this I want →
goes to worker1-staging instead

I have already registered a domain here at Cloudflare.
I’m still experimenting in the free plan.

This is what I think I need to utilise but I haven’t found out how.

Any help appreciated.

This looks very much like what I try to do:

So in essence I have deployed this with a few things replaced with dummy values in my example.

route = "*"
vars = { ENVIRONMENT = "dev" }

compatibility_date = "2022-10-12"

name = "my-worker-staging"
route = "*"
vars = { ENVIRONMENT = "staging" }

name = "my-worker"
route = "*"
vars = { ENVIRONMENT = "production" }

This results in having this table in Workers Routes section:

Route	                    Service	            Environment*	    my-worker-dev	    production*	        my-worker	        production*	my-worker-staging	production

Still if I make a GET to I get nothing in return even though it should respond with Hello world to any request.

Instead I get a Error: getaddrinfo EAI_AGAIN which indicates some DNS issue I assume.

In the end this might not be related to worker routes but instead my domain?
I haven’t configured anything in there and something tells me I need to setup an A record for root domain?
If so, what IP should go into the value? My workers are not running on a dedicated IP address, are they? As you can tell, DNS and routing is not my strong skill.

Ok. I think I managed to get it to work now.

For any future reference, if someone else is struggling with the same problem.
I setup A records in the DNS configuration for root and all subdomains.
I went with one level subdomains only since that seemed to play best with TLS certificates out of the box.

A api proxy
A api-staging proxy
A api-dev proxy
A @ proxy

That seems to work well and solves my current use case.
I’m not sure what really means in this context but that seemed to be best practice.

You seem to have got it working so you might not need this, but yes how Workers originally worked was they were not the origin. They would always run before the origin (the A record or CNAME). And hence yes, you needed to add a DNS record (orange cloud/proxied). Its value could be presumably anything (e.g your 192.x), as it did not matter. Since your Worker would handle the request to /*.

Cloudflare have since added custom domains and the Worker can be the origin. So they add the DNS records for you. So this may be of interest: