2016-04-16 36 views
6

我要代理/ V1/*到http://myserver.com,这里是我的脚本的WebPack-DEV-服务器代理这么想的工作

devServer: { 
 
    historyApiFallBack: true, 
 
    // progress: true, 
 
    hot: true, 
 
    inline: true, 
 
    // https: true, 
 
    port: 8081, 
 
    contentBase: path.resolve(__dirname, 'public'), 
 
    proxy: { 
 
    '/v1/*': { 
 
     target: 'http://api.in.uprintf.com', 
 
     secure: false 
 
     // changeOrigin: true 
 
    } 
 
    } 
 
},

,但它不工作, enter image description here

+0

你需要把配置放在哪里? – zehelvion

回答

10

更新: 感谢@chimurai,设置changeOrigin: true重要的是要使它发挥作用。

Underneathwebpack-dev-server将所有代理配置从documentation传递到http-proxy-middleware。很明显你想使用情况实际上与/v1/**路径实现:

devServer: { 
    historyApiFallBack: true, 
    // progress: true, 
    hot: true, 
    inline: true, 
    // https: true, 
    port: 8081, 
    contentBase: path.resolve(__dirname, 'public'), 
    proxy: { 
    '/v1/**': { 
     target: 'http://api.in.uprintf.com', 
     secure: false, 
     changeOrigin: true 
    } 
    } 
}, 
+1

感谢您的回答,但它也不奏效。 –

+5

应该使用代理选项:'changeOrigin:true' – chimurai

+0

'changeOrigin:true/false,默认值:false - 将主机头的原点更改为目标URL“是的,它确实有效。感谢您的回答! –

1

确保您请求的URL和端口匹配,你的的WebPack-dev的服务器上运行。因此,如果您的api位于http://localhost:5000,并且您的dev服务器正在运行http://localhost:8080,请确保您的所有请求都是http://localhost:8080。最好将您的请求发送到localhost:8080/api(以避免与应用程序路由冲突),并使用路径重写来删除/ api。

实施例:

的WebPack devserver代理配置:上

的WebPack dev的服务器运行:

http://localhost:8080 

期望API端点:

http://localhost:5000/items 

在您的应用中,请求:

http://localhost:8080/api/items

应该工作。在我看来,所有上述问题都来自向API url和端口发出请求,而不是webpack dev服务器url和端口,并使用代理和路径重写来将请求发送到API。

相关问题