【问题标题】:Creating a expressjs proxy server between webpack and API在 webpack 和 API 之间创建 expressjs 代理服务器
【发布时间】:2016-05-30 11:40:40
【问题描述】:

您好,我正在使用 webpack 创建一个 Web 应用程序,它使 REST api 调用后端服务器。我遇到的问题是 CORS 问题,所以我需要使用代理。

这导致我如何将运行在端口 (8080) 上的 wepback-dev-server 连接到运行在端口 (7000) 上的 api 服务器?我的代理服务器会和端口(8080)一样运行吗?

我阅读了 expressjs、npm node-http-proxy 和 webpack,但很难将它们结合在一起。

我是代理新手。

【问题讨论】:

    标签: api express web-applications webpack http-proxy


    【解决方案1】:

    下面是 webpack-dev-server 的示例配置,请参阅代理选项

    var config = {
    
      // webpack stuff here ...
    
      //dev server configuration
      devServer: {
    
        // ...
    
        // every request made to 'locahost:8080/api/xxxx' will be proxyfied to 'http://localhost:7000/api/xxxx'
        proxy: {
          "/api/*": {
            target: "http://localhost:7000",
            secure: false,
            rewrite: function(req, options) {
              //you can handle rewrite here if you need to        
            }
          },
    
        }
      },
     // 
    };
    
    module.exports = config;
    

    如此处所述https://webpack.github.io/docs/webpack-dev-server.html#proxy

    希望对你有帮助,

    EDIT 对于 webpack-dev-server v 1.14.1 'rewrite' 仍然实现

    【讨论】:

    • 好的,谢谢!另一个问题是这在投入生产时是否有效?从我读到的 webpack-dev-server 仅用于开发环境。
    猜你喜欢
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    相关资源
    最近更新 更多