【发布时间】:2016-07-25 12:34:54
【问题描述】:
我正在尝试使用 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 的文档,但我无法弄清楚我在哪里设置错误。有什么想法吗?
【问题讨论】:
-
网址正确指向
localhost:8080,因为那是您的开发服务器,只有在请求到达那里后,开发服务器才会代理它(也就是将其发送到)您提供的外部网址。 devtools 永远不会注意到代理。您的请求会发生什么情况,promise.catch() 是否记录错误?它是什么? (而且你没有使用 vue-cli 的 webpack 模板?)
标签: proxy vue.js webpack-dev-server vue-resource