devServer.proxy

如果你有单独的后端开发服务器 API,并且希望在同域名下发送 API 请求 ,那么代理某些 URL 会很有用。
在 localhost:3000 上有后端服务的话,你可以这样启用代理:
请求到 /api/users 现在会被代理到请求 http://localhost:3000/api/users。

module.exports = {
  //...
  devServer: {
    proxy: {
      '/api': 'http://localhost:3000'  
    }
  }
};

如果你不想始终传递 /api ,则需要重写路径:

module.exports = {
  //...
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        pathRewrite: {'^/api' : ''}
      }
    }
  }
};

默认情况下,不接受运行在 HTTPS 上,且使用了无效证书的后端服务器。如果你想要接受,修改配置如下:

module.exports = {
  //...
  devServer: {
    proxy: {
      '/api': {
        target: 'https://other-server.example.com',
        secure: false
      }
    }
  }
};
发布了18 篇原创文章 · 获赞 5 · 访问量 6724

猜你喜欢

转载自blog.csdn.net/qq_28687183/article/details/86593140
今日推荐