我正在尝试使用 webpack-dev-server 代理配置将 api 请求发送到外部域,但我似乎无法让它工作。
这是我的配置:
var path = require('path')
module.exports = {
entry: './client/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public/assets'),
publicPath: 'assets'
},
devServer: {
contentBase: 'public',
proxy:{
'/api/v1*': {
target: 'http://laravelandwebpack.demo/',
secure: false
}
}
}
}
因此,每当我的应用程序使用 uri 发出请求时,/api/v1...
它都应该将该请求发送到http://laravelandwebpack.demo
.
在我的 Vue 应用程序中,我使用vue-resource
发出请求,并且默认所有请求都带有所需的 uri 前缀:
var Vue = require('vue')
Vue.use(require('vue-resource'))
new Vue({
el: 'body',
http: {
root: '/api/v1', // prefix all requests with this
headers:{
test: 'testheader'
}
},
ready: function (){
this.$http({
url: 'tasks',
method: 'GET'
}).then(function (response){
console.log(response);
}, function (response){
console.error(response);
})
}
})
URL 的构造正确,但它们仍然指向localhost:8080
哪个是 webpack-dev-server:
我阅读并重新阅读了 webpack-dev-server 的文档,但我无法弄清楚我在哪里设置错误。有任何想法吗?