honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
16.96k stars 470 forks source link

How to get request's remote client IP in Node.js? #2994

Open movahhedi opened 3 weeks ago

movahhedi commented 3 weeks ago

What is the feature you are proposing?

How can i get the IP of the remote client?

I've seen https://hono.dev/docs/helpers/conninfo but there isn't anything for Node.js.

In Koa, its ctx.request.ip. code

May be useful:

EdamAme-x commented 3 weeks ago

hi @movahhedi It depends on which deployment service you use.

movahhedi commented 3 weeks ago

Hello @EdamAme-x

Node.js, PM2, nginx/Apache.

NicoPlyley commented 3 weeks ago

Hi @movahhedi, You can pass the IP address as a header using nginx and a reverse proxy see here where this issue was discussed more

movahhedi commented 3 weeks ago

Isn't this feature good enough to be included in Hono [for Node]? Since it's there in Koa, It should be technically possible.

EdamAme-x commented 3 weeks ago

I don't think Node.js by itself is often used to build servers, what do you think? I think most would use a reverse proxy or a deployment service.

implement in Koa https://github.com/koajs/koa/blob/b89e19a27509c96d27af722f9951e0d72cb41eb5/lib/request.js#L455

EdamAme-x commented 3 weeks ago

If you implement it, you can add it to the getConninfo helper.

nakasyou commented 3 days ago

Hi @movahhedi, ConnInfo helper for Node.js has been released.

import { getConnInfo } from '@hono/node-server/conninfo'
KaelWD commented 2 days ago

getConnInfo doesn't seem super useful, koa's ctx.ip also resolves X-Forwarded-For which is most likely what you're looking for in a production system.

yusukebe commented 2 days ago

Indeed, though I'm not sure it's within the scope of ConnInfo Helper, I might need to support X-Forwarded-For.