方式一
在package.json中添加
"proxy": "http://localhost:4000",
方式二
安装http-proxy-middleware
$ npm install http-proxy-middleware --save
$ # 或
$ yarn add http-proxy-middleware
创建 src/setupProxy.js
const { createProxyMiddleware } = require('http-proxy-middleware')
module.exports = function (app) {
app.use(
createProxyMiddleware('/api', {
target: 'http://localhost:5000/',
changeOrigin: true,
pathRewrite: {
'^/api': '',
},
})
)
}
参考文档 注意该文档中的 const proxy = require('http-proxy-middleware')
应改为 const { createProxyMiddleware } = require('http-proxy-middleware')
, 否则会报错 “proxy is not a function”