1

I want to get ip from a post request.

var ip = req.ip || req.headers['x-forwarded-for'] || 
req.connection.remoteAddress || 
req.socket.remoteAddress ||
(req.connection.socket ? req.connection.socket.remoteAddress : null)

res.json({
  success: true,
  message: 'ip'+ip
});

but it returns a null value. I'm using on a serve(it isn't a localhost)

Is there any solution? all solutions returns null;

S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
  • 2
    Possible duplicate of [How to determine a user's IP address in node](https://stackoverflow.com/questions/8107856/how-to-determine-a-users-ip-address-in-node) – Andrey Jun 18 '19 at 08:40
  • @Andrey all solutions returns a `null` value. – S.M_Emamian Jun 18 '19 at 08:42
  • It doesn't mean question isn't a dublicate. There's multiple headers possible holding IP address. This package for example checks much more, you can try it: https://github.com/pbojinov/request-ip/blob/master/src/index.js – Andrey Jun 18 '19 at 08:44
  • @Andrey `request-ip` returns `null`. – S.M_Emamian Jun 18 '19 at 09:57

1 Answers1

0

If it's behind a reverse proxy like Nginx try using the the embedded variables from ngx_http_core_module. (At least that's what I'm doing years now).

Nginx configuration file

....
location / {
   proxy_set_header X-Real-IP $remote_addr;
....

Node.js

req.headers["x-real-ip"]
devio
  • 145
  • 4