H
Hono9mo ago
mathys

Client ip

How can I get the client ip, I need it for my rate limiter
6 Replies
Nico
Nico9mo ago
What runtime are you using?
mathys
mathysOP9mo ago
Bun.sh
Nico
Nico9mo 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
mathysOP9mo 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
Nico9mo 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
mathysOP9mo ago
Thanks again for the help

Did you find this page helpful?