Search code examples
http-proxywebpack-dev-server

Proxy only POST requests (or any other HTTP method) through Webpack Dev Server?


Is there any way to only allow POST requests to be proxied using Webpack Dev Server? My app uses /login for GET requests and unfortunately it is being proxied to my other host regardless of HTTP method.

    // 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
    });

Solution

  • Yes, there is. You can use bypass parameter.

    // 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