服务器环境

获取本机 IPv4 地址

/**
 * 获取本机 IPv4 地址
 * Reference: https://nodejs.org/dist/latest-v6.x/docs/api/os.html#os_os_networkinterfaces
 */
exports.getIPv4Addr = function() {
    const networkInterfaces = require('os').networkInterfaces();
    const addresses = [];

    Object.keys(networkInterfaces).forEach(function(networkInterface) {
        networkInterfaces[networkInterface].forEach(function(address) {
            if (address.internal === false && address.family === 'IPv4') {
                addresses.push(address.address);
            }
        });
    });
    return addresses.length > 0 ? addresses[0] : 'localhost';
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17