H
Hono8mo ago
mathys

Client ip

How can I get the client ip, I need it for my rate limiter
6 Replies
Nico
Nico8mo ago
What runtime are you using?
mathys
mathysOP8mo ago
Bun.sh
Nico
Nico8mo ago
It's not supported in Hono directly, bun you can do this
import app from './app';

Bun.serve({
fetch(req, res) {
globalThis.ip = res.requestIP(req);
return app.fetch(req, res)

},
});
import app from './app';

Bun.serve({
fetch(req, res) {
globalThis.ip = res.requestIP(req);
return app.fetch(req, res)

},
});
You can acess it with globalThis.ip. Until it's supported in Hono this is the best way @mathys Forget that I found a much better way
import app from './app';

Bun.serve({
fetch(req, server) {
return app.fetch(req, { ip: server.requestIP(req)})

},
});
import app from './app';

Bun.serve({
fetch(req, server) {
return app.fetch(req, { ip: server.requestIP(req)})

},
});
Now you can access it in c.env
mathys
mathysOP8mo ago
@Nico it's working for me with this return
{
address: "::1",
family: "IPv6",
port: 64615,
}
{
address: "::1",
family: "IPv6",
port: 64615,
}
I think you should add this to the doc maybe
Nico
Nico7mo ago
Perfect, once it’s live it will show an ip ::1 is just localhost. Or if you use another device you should see it And I will add it to the docs
mathys
mathysOP7mo ago
Thanks again for the help
Want results from more Discord servers?
Add your server