//在所有路由之前允许跨域
app.use((req,res,next)=>{
//后台对前端发发送过来的请求头,进行设置,允许他们跨域
res.header({
'Access-Control-Allow-Credentials':true,
'Access-Control-Allow-Origin':req.header.origin || '*',
'Access-Control-Allow-Headers':'Content-Type',
'Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS',
'Content-Type':'application/json;charset=utf-8'
})
})
//CROS跨域
配置代理服务器
module.exports = {
runtimeCompiler: true,
// publicPath: '/', // 设置打包文件相对路径
devServer: {
// open: process.platform === 'darwin',
// host: 'localhost',
// port: 8080,
// open: true, //配置自动启动浏览器
proxy: {//配置请求代理,转发api请求接口,方便上线.还可以配置跨域
'/api': {
target: 'http://127.0.0.1:3000', //对应自己的接口
ws: true, //是否启用websorke
secure:false,//http协议使用false,https使用true
changeOrigin: true,//开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样客户端端和服务端进行数据的交
pathRewrite: {
'^/api': '/api'
}
}
}
},
}