2016-04-16 41 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。

相關問題