admin管理员组文章数量:1410697
I am using this code to get the ip address in Node.js:
const ip = await (req.headers['x-forwarded-for'] || '').split(',').pop().trim() || req.socket.remoteAddress;
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
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.
How do I get the individual device ip address of each device in Node.js?
EDIT:
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.
But I need to get the device ip address. I do NOT want the connection ip address. I want the device ip address.
Here are the changes I made:
I set 'trust proxy' to true:
app.set('trust proxy', true);
I updated the etc/nginx/sites-available/mysite file to look like this:
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;
}
I updated the etc/nginx/proxy_params file to look like this:
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;
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.
I am using this code to get the ip address in Node.js:
const ip = await (req.headers['x-forwarded-for'] || '').split(',').pop().trim() || req.socket.remoteAddress;
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
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.
How do I get the individual device ip address of each device in Node.js?
EDIT:
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.
But I need to get the device ip address. I do NOT want the connection ip address. I want the device ip address.
Here are the changes I made:
I set 'trust proxy' to true:
app.set('trust proxy', true);
I updated the etc/nginx/sites-available/mysite file to look like this:
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;
}
I updated the etc/nginx/proxy_params file to look like this:
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;
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.
Share Improve this question edited Jun 6, 2021 at 11:41 Page COW asked May 31, 2021 at 18:29 Page COWPage COW 7653 gold badges18 silver badges41 bronze badges 11- While in case of routing, the IP Address is that of the router if the network is being directed through one and not of the device. Hence, you get the same IP Address for all your devices which is that of the router itself. I am not sure though but I believe this is the reason you are getting the same address. – Karan Gaur Commented May 31, 2021 at 18:36
- I thought that too at first. But the same ip address is shown when I am using my phone data, NOT my router / wifi internet. I also just asked someone and found out it shows the same ip address for a phone not on my wifi. – Page COW Commented May 31, 2021 at 20:10
-
Did you try
req.connection.remoteAddress
? – Cássio Lacerda Commented May 31, 2021 at 20:50 - See if this helps:- stackoverflow./questions/10849687/… – Karan Gaur Commented Jun 1, 2021 at 8:26
- 2 Might help if you stated an example what IP address you expect to get - or at least what type or range. If you're trying to get an address that's been through NAT (e.g. my machine has an address of 192.168.0.101) and you want that to be returned from the XFF headers for a website hosted on the public internet, then you're asking for the impossible. Same applies to any in the private ranges. See en.wikipedia/wiki/Private_network – Richard Wheeldon Commented Jun 6, 2021 at 14:41
4 Answers
Reset to default 2 +50What you could be looking for is Local Area Network Ip address:
You could use default function by Node.js osworkInterfaces()
You could find the documentation here:
https://nodejs/api/os.html#os_os_networkinterfaces
You could also look into this thread:
Get local IP address in Node.js
I found another solution for my use case. Based on some people's ments, it looks like it may not be possible to find the private ip address of a device that connects to your website, only the public ip address.
@Cerceis osworkInterfaces() answer may work. I did a quick test, but was unable to know for sure if it works. I don't have time to test it out more fully. If you are hoping to find an answer, I would try out osworkInterfaces() in Node.js and that might get you the ip address you're looking for.
You can use the Native os
module in Node.js. It provides all the operating system-related utility methods and properties.
var os = require('os');
var allNetworkInterfaces = osworkInterfaces();
console.log(allNetworkInterfaces);
The call to osworkInterfaces() will return an object containing network interfaces that have been assigned a network address. The output of the above code looks like this:
{
lo: [
{
address: '127.0.0.1',
netmask: '255.0.0.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: true,
cidr: '127.0.0.1/8'
},
{
address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 0,
internal: true,
cidr: '::1/128'
}
],
eth0: [
{
address: '192.168.1.108',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '01:02:03:0a:0b:0c',
internal: false,
cidr: '192.168.1.108/24'
},
{
address: 'fe80::a00:27ff:fe4e:66a1',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '01:02:03:0a:0b:0c',
scopeid: 1,
internal: false,
cidr: 'fe80::a00:27ff:fe4e:66a1/64'
}
]
}
Finding the IP address is Node.js
One of the easiest methods of finding the IP address is to use the "ip" NPM module. It is super quick and easy (and it has worked for me in the past)
const ip = require('ip')
console.log(ip.address())
Learn more at: https://www.npmjs./package/ip
本文标签: javascriptHow to get the ip address in Nodejs ExpressStack Overflow
版权声明:本文标题:javascript - How to get the ip address in Node.js Express? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744906516a2631656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论