方法一:
node 14之前的
require('os').networkInterfaces()[Object.keys(require('os').networkInterfaces())[0]][1].address + ':8080'
vue.config.js
module.exports = { lintOnSave: false, // 是否开启eslint保存检测,有效值:ture | false | 'error' publicPath: '', outputDir: process.env.outputDir, devServer: { public: require('os').networkInterfaces()[Object.keys(require('os').networkInterfaces())[0]][1].address + ':8080', disableHostCheck: true, open: true, proxy: { '/api': { target: 'http://xxx.com', pathRewrite: {'^/api' : ''}, changeOrigin: true, secure: false, }, } }, };
方法二:
node 14 之后的
const os = require('os'); /** * 获取当前机器的ip地址 */ function getIpAddress() { let ifaces = os.networkInterfaces() for (let dev in ifaces) { let iface = ifaces[dev] for (let i = 0; i < iface.length; i++) { let { family, address, internal } = iface[i] if (family === 'IPv4' && address !== '127.0.0.1' && !internal) { return address } } } }
vue.config.js
const os = require('os'); /** * 获取当前机器的ip地址 */ function getIpAddress() { let ifaces = os.networkInterfaces() for (let dev in ifaces) { let iface = ifaces[dev] for (let i = 0; i < iface.length; i++) { let { family, address, internal } = iface[i] if (family === 'IPv4' && address !== '127.0.0.1' && !internal) { return address } } } } module.exports = { lintOnSave: false, // 是否开启eslint保存检测,有效值:ture | false | 'error' publicPath: '', outputDir: process.env.outputDir, devServer: { public: getIpAddress() + ':8080', disableHostCheck: true, open: true, proxy: { '/api': { target: 'http://mh.885505.com', pathRewrite: {'^/api' : ''}, changeOrigin: true, secure: false, }, } }, };
效果:
注: 我之前用的是node 8、 node 12 用的第一种方法,后来升级node 14 运行时报错了 找不到 address , 方法二可以;
标签:node,false,os,ifaces,ip,let,address,本机 From: https://www.cnblogs.com/wangyihong/p/16965765.html