如何在webpack-dev-server的代理配置中进行代理上下文匹配时分配函数?

时间:2016-10-11 09:18:18

标签: webpack webpack-dev-server

例如我的webpack.config.js

module.exports = {
    devServer: {
        proxy: {
            '/api': {
                target: 'http://localhost:3000',
                pathRewrite: {'/api': ''}
            }
        }
    },
    ....
}

在这里,我使用/api来匹配一个网址规则。但我查看了http-proxy-middleware webpack-dev-server的基础proxy,它说我们可以使用filter function来执行custom match }:

var filter = function (pathname, req) {
    return (pathname.match('^/api') && req.method === 'GET');
};
var apiProxy = proxy(filter, {target: 'http://www.example.org'})

但是在webpack.config.js中,我如何使用filter function来执行custom match?感谢。

1 个答案:

答案 0 :(得分:0)

devServer: {
    proxy: [
        {
            target: 'http://api.example.com',
            context: function(pathname, req) {
                if (/^\/api/.test(pathname)) {
                    return true;
                }
                return false;
            }
        }
    ]
}

参考:https://webpack.github.io/docs/webpack-dev-server.html#proxy