Is there any way to strip route path prefix from workers?

Hello!

I set up 3 routes to a Cloudflare worker, just like this:

example1.com/*
example2.com/api-v1/*
example3.com/api/v2/*

Among different routes, there are different path layers. But they do the same things in the asterisk part. Like calling example1.com/?foo=bar is same as example2.com/api-v1/?foo=bar

But in the Cloudflare worker API, I can only get the full URL but can’t get what is the route parts of the URL. I want to get the route part of the request URL, as the logic of the left part of the URL is the same among all these routes.

At this time, I can only create an additional environment variable to store all routes and add a check code to match the environment variable. When I update the routes, I also need to update the environment variables.

Is there any way to let the worker program get the asterisk part whatever the route is, except hardcoding routes in the program or env?

No, it’s not exposed or anything. You’d have to have that sort of logic in your worker, perhaps checking for specific known paths and stripping (ex: support two setups, either on base or on /customname and check if the path starts with /customname and stripping if needed