打開node_modules/webpack-dev-server/lib/Server.js文件
1:修改disallow=true
Server.prototype.checkHost = function(headers) {
// allow user to opt-out this security check, at own risk
if(this.disableHostCheck) return true;
// get the Host header and extract hostname
// we don't care about port not matching
const hostHeader = headers.host;
if(!hostHeader) return false;
const idx = hostHeader.indexOf(":");
const hostname = idx >= 0 ? hostHeader.substr(0, idx) : hostHeader;
// always allow localhost host, for convience
if(hostname === "127.0.0.1" || hostname === "localhost") return true;
// allow hostname of listening adress
if(hostname === this.listenHostname) return true;
// also allow public hostname if provided
if(typeof this.publicHost === "string") {
const idxPublic = this.publicHost.indexOf(":");
const publicHostname = idxPublic >= 0 ? this.publicHost.substr(0, idxPublic) : this.publicHost;
if(hostname === publicHostname) return true;
}
// disallow
return true;
}
2、啓動時候,使用ng serve —host 0.0.0.0
問題解決。