Get local IP address in Node.js
This information can be found in os.networkInterfaces()
, — an object, that maps network interface names to its properties (so that one interface can, for example, have several addresses):
'use strict';
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
const results = Object.create(null); // Or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
// 'results'
{
"en0": [
"192.168.1.101"
],
"eth0": [
"10.0.0.101"
],
"<network name>": [
"<ip>",
"<ip alias>",
"<ip alias>",
...
]
}
// results["en0"][0]
"192.168.1.101"
How to determine a user's IP address in node
In your request
object there is a property called socket
, which is a net.Socket
object. The net.Socket
object has a property remoteAddress
, therefore you should be able to get the IP with this call:
request.socket.remoteAddress
(if your node version is below 13, use the deprecated now request.connection.remoteAddress
)
EDIT
As @juand points out in the comments, the correct method to get the remote IP, if the server is behind a proxy, is request.headers['x-forwarded-for']
EDIT 2
When using express
with Node.js:
If you set app.set('trust proxy', true)
, req.ip
will return the real IP address even if behind proxy. Check the documentation for further information
How to get the ip address in Node.js Express?
I found another solution for my use case. Based on some people's comments, 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 os.networkInterfaces() 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 os.networkInterfaces() in Node.js and that might get you the ip address you're looking for.
How can I get the local IP address in Node.js?
http://nodejs.org/api/os.html#os_os_networkinterfaces
var os = require('os');
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
console.log(addresses);
Get local IP address in Node.js
This information can be found in os.networkInterfaces()
, — an object, that maps network interface names to its properties (so that one interface can, for example, have several addresses):
'use strict';
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
const results = Object.create(null); // Or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
// 'results'
{
"en0": [
"192.168.1.101"
],
"eth0": [
"10.0.0.101"
],
"<network name>": [
"<ip>",
"<ip alias>",
"<ip alias>",
...
]
}
// results["en0"][0]
"192.168.1.101"
Get local network ip addresses on NodeJS server
local-devices
Npm
npm install local-devices
Example
// Using a transpiler
import find from 'local-devices'
// Without using a transpiler
const find = require('local-devices');
// Find all local network devices.
find().then(devices => {
devices /*
[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
*/
})
Related Topics
Invalid Hook Call. Hooks Can Only Be Called Inside of the Body of a Function Component
Generating Non-Repeating Random Numbers in Js
Convert String in Dot Notation to Get the Object Reference
How to Do Outerhtml in Firefox
What's the Cause of the Error 'Getaddrinfo Eai_Again'
How to Use Onclick() or Onselect() on Option Tag in a Jsp Page
Using Http Rest APIs with Angular 2
How to Change the Content of a <Textarea> with JavaScript
Anonymous Class Instance - Is It a Bad Idea
When Does Js Interpret {} as an Empty Block Instead of an Empty Object
Javascript: Which Browsers Support Parsing of Iso-8601 Date String with Date.Parse
What Is the Correct Term for Variable Shadowing in JavaScript
How to Map More Than One Property from an Array of Objects
Cloud Firestore Case Insensitive Sorting Using Query