我想将/v1/*代理给http://myserver.com,下面是我的脚本
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
},
},
},
但它不起作用,
发布于 2016-04-16 17:14:29
更新:多亏了@chimurai,设置changeOrigin: true
对它的工作很重要。
Underneath webpack-dev-server
将所有代理配置从documentation传递给http-proxy-middleware
。很明显,您想要的用例实际上是通过/v1/**
path实现的:
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
}
}
},
发布于 2017-09-14 00:34:32
确保您的请求url和端口与运行您的webpack-dev-server的url和端口匹配。因此,如果您的api位于http://localhost:5000
,并且您的开发服务器运行在http://localhost:8080
上,请确保所有请求都是针对http://localhost:8080
的。最好是向localhost:8080/api
发出请求(以避免与应用程序路由冲突),并使用路径重写来删除/api。
示例:
Webpack devserver代理配置:
proxy: {
'/api': {
target: 'http://localhost:5000',
pathRewrite: { '^/api': '' },
},
}
运行在以下平台上的开发服务器:
http://localhost:8080
所需的API端点:
http://localhost:5000/items
在您的应用程序中,向以下位置发出请求:
http://localhost:8080/api/items
。
这应该是可行的。在我看来,所有上述问题都源于对API和端口的请求,而不是对webpack开发服务器的url和端口的请求,并使用代理和路径重写将请求定向到API。
发布于 2018-08-22 17:48:15
这对我来说很好。
devServer: {
host: '11.11.111.111', //local ip
port: 8080,
contentBase: outputpath,
historyApiFallback: true,
inline: true,
proxy: {
'/api':{
target:'http://example.com',
pathRewrite: {'^/api' : ''},
secure:false,
changeOrigin:true
}
}
},
//使用
$.ajax({
url:'/api/pvp/share/getsharecfg.php',
dataType:'json',
...
https://stackoverflow.com/questions/36662065
复制相似问题