使用webpack-dev-server的代理功能
const path = require('path');
module.exports = {
// ...其他配置
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000,
proxy: {
'/api': {
target: 'http://localhost:5000', // 目标服务器地址
changeOrigin: true, // 对于虚拟托管的网站,这个项需要设置为true
pathRewrite: { '^/api': '' }, // 重写路径
}
}
}
};
在这个示例中,任何以 /api 开头的请求都会被代理到 http://localhost:5000。