Cloudflare Workers URL Path

Hello, is it posible to get URL parameters? Like this for example:

https://products.domain.workers.dev/this-is-the-products-name-slug/

And latter get the slug “this-is-the-products-name-slug” and then search on Cloudflare Workers KV?

I got this working:
https://products.domain.workers.dev/?slug=this-is-the-products-name-slug

But not the other solution. I can’t find anything on the documentation about this.

It depends what you call a parameter. Typically the value parts of the query string (if there are such) are called parameters. What you seem to be referring to here is simply the path of the URL.

You can extract all of that from the url field of the request object. Respectively parse its details by passing it to a new URL object. That is not so much Cloudflare related, but general JavaScript development.

But I get an error if I access that URL, should I include something when creating the worker that allows to have different paths?

Can you post the code and the error?

Use regex as an option;

async function handleRequest(request) {
      var uri = request.url.replace(/^https:\/\/.*?\//gi, "/");
    console.log(uri);

UPD. with URL object.

 async function handleRequest(request) {
     const url = new URL(request.url);
     uri = url.pathname;
     console.log(uri);