在 node.js 中,IP 地址不断变化。但我认为本地计算机 IP 地址是不变的

时间:2021-05-07 23:03:04

标签: node.js ip ip-address

我只需要根据他们的计算机了解用户,而不是使用用户注册到数据库。我认为使用计算机的 ip 地址会有所帮助,但如果有其他选择,我愿意了解它。这是我正在使用的代码:

const { networkInterfaces } = require('os');

const getLocalIP = () => {
    const nets = networkInterfaces();
    const results = Object.create(null);

    for (const name of Object.keys(nets)) {
        for (const net of nets[name]) {
            if (net.family === 'IPv4' && !net.internal) {
                if (!results[name]) {
                    results[name] = [];
                }
    
                results[name].push(net.address);
                return net.address
            }
        }
    }
}

exports.getLocalIP = getLocalIP

0 个答案:

没有答案