react項目設置代理(proxy)

  • 安裝http-proxy-middleware

npm install http-proxy-middleware

  • 在src下建立setupProxy.js文件
const proxy = require("http-proxy-middleware");
module.exports = function(app) {
  app.use(
    proxy("/api", {
      target: <代理地址>,
      changeOrigin: true, // needed for virtual hosted sites
      ws: true, // proxy websockets
      pathRewrite: {
        "^/api": ""
      }
    })
  );
};

重啓服務,如果後端服務爲Https,使用 HTTPS=true npm start 啓動

💬官網說明-proxying-api-requests-in-development

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章