配置 devserve 配置
module.exports = {
devServer: {
host: '0.0.0.0', // 让你的服务器可以被外部访问
port: 8080, // 端口
https: false, // 不使用 HTTP 提供服务
hot: true, // 模块热替换
open: true, // 打开浏览器
client:{
overlay: { // 当出现编译错误或警告时,在浏览器中显示全屏覆盖。
warnings: true,
errors: true
},
progress: true, // 在浏览器中以百分比形式打印编译进度。
},
headers: { // 设置请求头
'X-Custom-Header': 'value'
},
compress: true, // 启用 gzip 压缩
proxy: {
'/api': { // 对 /api 的请求会将请求代理到 http://localhost:4000。
target: 'http://localhost:4000',
changeOrigin: true, // 使 Origin 请求头中的主机名变为目标 URL 的主机名
secure: false, // 不验证 SSL 证书
logLevel: 'warn', // 代理的日志级别
ws: true, // 使用 WebSockets 作为服务器
pathRewrite: { // 重写路径
'^/api': ''
},
router: function(req) {
if (req.url.startsWith('/api')) {
return '/api';
}
return false;
},
context: ['/api'],
bypass: function(req) {
if (req.headers.accept.indexOf('html') !== -1) {
return '/';
}
}
}
},
contentBase: './public', // 开发服务器提供的静态文件目录
historyApiFallback: true, // 是否启用 HTML5 history API 的回退
before: function(app, server) {
// 在服务器启动之前执行的函数
},
after: function(app, server) {
// 在服务器启动之后执行的函数
},
setupMiddlewares: true, // 设置中间件
}
};