I'm working with Cloudflare Workers and
I'm working with Cloudflare Workers and Durable Objects (DO) and have a question about handling request forwarding. Specifically, I'm trying to understand the best way to forward request configurations such as the HTTP method, headers, and other details to a DO worker.
Is there a specific API or method in Cloudflare Workers for directly forwarding these request configurations to a DO? Or do I need to manually extract these details from the incoming request, create a new request object with these values, and then pass this object to the DO?
2 Replies
This code is for proxying from Cloudflare Pages, not Workers, but it should be very similar from a Worker. https://github.com/transformation-dev/blueprint/blob/master/packages/cloudflare-do-utils/src/pages-do-proxy.js
GitHub
blueprint/packages/cloudflare-do-utils/src/pages-do-proxy.js at mas...
Contribute to transformation-dev/blueprint development by creating an account on GitHub.
Also, keep in mind that you can pass in the original request to the constructor for a new Request like this.
newRequest = new Request('https://new.host', {...originalRequest, headers: { a: '1' } })
So, you only need to specify things you want to change when creating the new Request.
That said, in my proxy code at the start of this thread, I just pass the raw request down to the DO. The URL is not used for routing to the DO so you can leave it whatever it was... or change it if you want.