方式一

在package.json中添加

  1. "proxy": "http://localhost:4000",

方式二

安装http-proxy-middleware

  1. $ npm install http-proxy-middleware --save
  2. $ # 或
  3. $ yarn add http-proxy-middleware

创建 src/setupProxy.js

  1. const { createProxyMiddleware } = require('http-proxy-middleware')
  2. module.exports = function (app) {
  3. app.use(
  4. createProxyMiddleware('/api', {
  5. target: 'http://localhost:5000/',
  6. changeOrigin: true,
  7. pathRewrite: {
  8. '^/api': '',
  9. },
  10. })
  11. )
  12. }

参考文档 注意该文档中的 const proxy = require('http-proxy-middleware') 应改为 const { createProxyMiddleware } = require('http-proxy-middleware'), 否则会报错 “proxy is not a function”