any way to use the binding with hono without passing the service url?

I am using several workers and I would like to use one as a gateway and communicate with all of them through service bindings, but is there a way to use the service binding without passing the full URL of the other service? I attach the example
const app = new Hono<{ Bindings: { USERS_WORKER: Fetcher } }>();

app.get("/", async (c) => {
const res = await c.env.USERS_WORKER.fetch("/");

const data = await res.json();

console.log(data);

return c.text("Hello Hono!");
});
const app = new Hono<{ Bindings: { USERS_WORKER: Fetcher } }>();

app.get("/", async (c) => {
const res = await c.env.USERS_WORKER.fetch("/");

const data = await res.json();

console.log(data);

return c.text("Hello Hono!");
});
i get this error ✘ [ERROR] TypeError: Invalid URL: / only works if i set the url like this const res = await c.env.USERS_WORKER.fetch("http://localhost:8787/"); But since I have several workers, I'd need to define many variables and I'd like to know if there's a way to save myself that step.
7 Replies
Cyb3r-Jak3
Cyb3r-Jak33mo ago
I believe you should be able to do something like const res = await c.env.USERS_WORKER.fetch("http://example/");
lucasp
lucaspOP3mo ago
Yes, but I wanted to know if I should do it without putting the URL, since I have several workers and this would save me from having one more variable.
Cyb3r-Jak3
Cyb3r-Jak33mo ago
Sorry should of clarified. I don't believe it just has to be a valid URL so for all your requests it could be const res = await c.env.USERS_WORKER.fetch("http://example/<path>"); . I don't think you need to worry about it being a valid hostname
lucasp
lucaspOP3mo ago
I got it and it worked! :O. Will it work the same in production???
Cyb3r-Jak3
Cyb3r-Jak33mo ago
It should do.
lucasp
lucaspOP3mo ago
ty dude!
Radoš
Radoš3mo ago
You can use RPC, so you do not have to use fetch and also, you do not have to make it public (no public url). https://developers.cloudflare.com/workers/runtime-apis/rpc/ https://blog.cloudflare.com/javascript-native-rpc/ https://github.com/cloudflare/js-rpc-and-entrypoints-demo
Want results from more Discord servers?
Add your server