仅通过Webpack Dev Server代理POST请求(或任何其他HTTP方法)?

时间:2016-08-11 01:52:58

标签: http-proxy webpack-dev-server

有没有办法只允许使用Webpack Dev Server代理POST请求?我的应用程序使用/ login进行GET请求,不幸的是,无论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
    });

1 个答案:

答案 0 :(得分:4)

是的,有。您可以使用旁路参数。

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