Skip to content

if you want dev by ip, please set host: '0.0.0.0',没有用。 #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
seayao opened this issue Nov 22, 2018 · 7 comments
Closed

if you want dev by ip, please set host: '0.0.0.0',没有用。 #1354

seayao opened this issue Nov 22, 2018 · 7 comments
Labels

Comments

@seayao
Copy link

seayao commented Nov 22, 2018

qq 20181122141258
然后浏览器将会打开在: 'http://0.0.0.0:9527/'.
但是 0.0.0.0 是无法访问的。

@PanJiaChen
Copy link
Owner

你重启过服务么?

@seayao
Copy link
Author

seayao commented Nov 26, 2018

是的,重启过,
我确信,
因为就在刚刚我试了一下。

@PanJiaChen
Copy link
Owner

服务启动成功了么,我刚刚在我机器上(mac)上试了下没啥问题。

@seayao
Copy link
Author

seayao commented Nov 26, 2018

为了确保代码正确,
我刚刚又重新下载了代码并install了一下,
host改成0.0.0.0,npm run dev之后,
仍然无效(windows),
难道是操作系统的问题?
qq 20181126184239

@PanJiaChen
Copy link
Owner

PanJiaChen commented Nov 27, 2018

devServer: {
    contentBase: '.',
    host: '0.0.0.0',
    port: 3000,
    disableHostCheck: true
  },

或者去这个issue里面找找问题
webpack/webpack-dev-server#147

我这里不能重现,所以也实在是帮不了你什么。

@seayao
Copy link
Author

seayao commented Dec 3, 2018

我找到问题所在了,
其实把host设置为0.0.0.0是可以在localhost:post或者yourip:host打开项目的
但是由于设置了autoOpenBrowser为true
浏览器会自动打开在0.0.0.0:host导致出现上述问题
我将webpack.dev.conf.js的代码改正如下:
加入获取本机ip地址函数:
function getIPAddress(){ const interfaces = require('os').networkInterfaces(); for(let devName in interfaces){ let iface = interfaces[devName]; for(let i=0;i < iface.length;i++){ let alias = iface[i]; if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) { return alias.address; } } } }
如图:
qq 20181203101641
并修改npm run dev成功后的提示信息:
let messages = [] if (devWebpackConfig.devServer.host === '0.0.0.0') { messages = [Your application is running at:\n\t- Local: http://localhost:${port}\n\t- Network: http://${getIPAddress()}:${port}] } else if (devWebpackConfig.devServer.host === 'localhost') { messages = [Your application is running at:\n\t- Local: http://localhost:${port}`]
} else {
messages = [Your application is running at:\n\t- Network: http://${getIPAddress()}:${port}]
}`
如图:
qq 20181203101657
运行成功后的效果图:
qq 20181203102227
但是我还是没有找到如何修改浏览器在0.0.0.0自动打开的地址,
还请你有时间可以告知一下,
感谢。

@PanJiaChen
Copy link
Owner

感谢你的回答,之后用vue-cli@3.0了,就不会有这个这问题了。

@PanJiaChen PanJiaChen added the 4.0 label Dec 3, 2018
@seayao seayao closed this as completed Dec 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants