2016-08-11 5 views
1

Webpack Dev Serverを使用してPOST要求のみをプロキシできるようにする方法はありますか?私のアプリはGETリクエストに/ loginを使用していますが、残念ながらHTTPメソッドに関係なく他のホストにプロキシされています。Webpack Dev Serverを介したPOSTリクエスト(または他のHTTPメソッド)のみをプロキシしますか?

// Serve the Relay app 
    const compiler = webpack(config); 
    appServer = new WebpackDevServer(compiler, { 
     contentBase: '/public/', 
     proxy: { 
      '/login': `http://localhost:${GRAPHQL_PORT}`, // only for POST? 
     }, 
     publicPath: '/js/', 
     stats: { 
      colors: true, 
      chunks: false, 
     }, 
     historyApiFallback: true 
    }); 

答えて

3

はい、あります。バイパスパラメータを使用できます。

// Serve the Relay app 
const compiler = webpack(config); 
appServer = new WebpackDevServer(compiler, { 
    contentBase: '/public/', 
    proxy: { 
     '/login': { 
      target: `http://localhost:${GRAPHQL_PORT}`, // only for POST? 
      bypass: function(req, res, proxyOptions) { 
       if(req.method != 'POST') return false; 
      } 
     } 
    }, 
    publicPath: '/js/', 
    stats: { 
     colors: true, 
     chunks: false, 
    }, 
    historyApiFallback: true 
}); 

documentation Webpack 1

documentation Webpack 2

+1

バイパス署名が '(REQ、RES)'しない '(RES、REQ)'です。 'bypass:req => req.method!== 'POST'? false:undefined'で十分です。 – greenimpala

+1

@greenimpalaあなたは正しいです、これを指摘してくれてありがとう。私はもっ​​と冗長に(IMHO初心者のためのより簡単な)バージョンに固執します。 – mleko

関連する問題