VUE-获取本地局域网IP地址

const os = require('os'); //提供本地的相关信息

// 获取本地ip
function getNetWorkIp() {
    // 打开host
    let needHost = '';
    try {
        let network = os.networkInterfaces();
        for (const dev in network) {
            let iface = network[dev];
            for (let i = 0; i < iface.length; i++) {
                const alias = iface[i];
                if (
                    alias.family === 'IPv4' &&
                    alias.address !== '127.0.0.1' &&
                    !alias.internal
                ) {
                    needHost = alias.address;
                }
            }
        }
    } catch (error) {
        needHost = 'http://localhost';
    }
    return needHost
}
const IP = getNetWorkIp();

//跨域处理
    devServer: {
        host: IP,   //这样就可以动态获取
        proxy: {
            '/api': {
                target: `${IP}:3000/api`,
            }
        }
    },

你可能感兴趣的:(vue,js,tcp/ip,网络协议,网络)