Search code examples
node.jsip-address

Get local network ip addresses on NodeJS server


I need to get the info.(private ip address and MAC) of who made a request to a nodeJS server. This is running on an intranet, so i suppose i could get the MAC data somehow, and the ip from os.networkinterfaces() or req.connection isnt useful to me since they give me the server or the public ip respectively.


Solution

  • 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: '...' }
      ]
      */
    })