Service binding fetcher misunderstanding

I am struggling to call a worker from my pages, everything is setup but I don't understand what I need to pass to the fetcher. My code looks like this currently
const newVisitReq = await env.LOGDB_API.fetch("http://localhost:8787", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
timestamp: "2024-03-10T11:12:54.178Z",
linkId: 123,
longitude: 12.34,
listId: 1,
latitude: 56.78,
url: "https://example.com",
pathname: "/path/to/resource",
region: "Some Region",
country: "Some Country",
referer: "https://referrer.com",
timezone: "UTC",
city: "Some City",
userAgent: "Mozilla/5.0",
xForwardedFor: "192.0.2.1",
continent: "Some Continent",
postalCode: "12345",
colocation: "Some Colocation",
outcome: "Success",
host: "example.com",
}),
});
const newVisitReq = await env.LOGDB_API.fetch("http://localhost:8787", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
timestamp: "2024-03-10T11:12:54.178Z",
linkId: 123,
longitude: 12.34,
listId: 1,
latitude: 56.78,
url: "https://example.com",
pathname: "/path/to/resource",
region: "Some Region",
country: "Some Country",
referer: "https://referrer.com",
timezone: "UTC",
city: "Some City",
userAgent: "Mozilla/5.0",
xForwardedFor: "192.0.2.1",
continent: "Some Continent",
postalCode: "12345",
colocation: "Some Colocation",
outcome: "Success",
host: "example.com",
}),
});
But I fail to understand what URL I need to give the fetcher to call my worker instead of my local url and every example I found only cloned the request received by pages and passed it on. Which is not what I am trying to do. The worker I am trying to access takes the form of a REST API so I can't just pass my request.
4 Replies
K
K3mo ago
Not sure if it's an correct answer but any url works.
Lotrez
Lotrez3mo ago
So I should point to my worker URL? How can I get that from a pages function?
K
K3mo ago
So I should point to my worker URL? Yes. How can I get that from a pages function? You can just write it in your code. When deploying your worker you get yourproject.workers.dev.
Fineshop Design
Fineshop Design3mo ago
I use:
const response = await env.FETCHER.fetch(new URL("/worker-path-to-request", request.url), {...});
const response = await env.FETCHER.fetch(new URL("/worker-path-to-request", request.url), {...});