title: How to get the ip address in Node.js Express?
body: <p>I am using this code to get the ip address in Node.js:</p>
  <pre><code>const ip = await (req.headers['x-forwarded-for'] || '').split(',').pop().trim() || req.socket.remoteAddress;
  </code></pre>
  <p>For all the devices on my home wifi network and when I access my website using data on my phone, I get this ip address: ::ffff:127.0.0.1</p>
  <p>I'm trying to get the ip address of each individual device (phone, laptop) that visits my site. But all of the devices show the same ip address.</p>
  <p>How do I get the individual device ip address of each device in Node.js?</p>
  <p><strong>EDIT:</strong></p>
  <p>I made some updates and no longer get ::ffff:127.0.0.1. I now get the ip address of the internet connection. So if I'm connected to wifi, I get the wifi modem ip address. If I'm using data, I get the data connection ip address.</p>
  <p><strong>But I need to get the device ip address. I do NOT want the connection ip address. I want the device ip address.</strong></p>
  <p>Here are the changes I made:</p>
  <p>I set 'trust proxy' to true:</p>
  <pre><code>app.set('trust proxy', true);
  </code></pre>
  <p>I updated the etc/nginx/sites-available/mysite file to look like this:</p>
  <pre><code>location / {
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:5050;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
  </code></pre>
  <p>I updated the etc/nginx/proxy_params file to look like this:</p>
  <pre><code>proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_set_header X-Forwarded-Proto $scheme;
  </code></pre>
  <p>What did I do wrong? How do I fix this? From what I'm reading, it sounds like I should be able to use req.headers['x-forwarded-for'] to get the right ip address, but req.headers['x-forwarded-for'] returns the same thing as req.headers['x-real-ip'] except it is in an array.</p>