iview-project
iview-project copied to clipboard
如何代理请求
在使用该项目的时候 需要设置请求代理,按照如下配置。
webpack.dev.config.js
希望把页面上 /api相关的请求都代理到http://local.engineer.com:5000这个路径下,
config.devServer = {
inline: true,
proxy: {
'/api*': {
xfwd: true,
// host: '127.0.0.1:3002',
target: 'http://local.engineer.com:5000',
bypass: function (req, res, proxyOptions) {
console.log('Proxy: ' + req.url);
}
}
}
}
但是结果,请求还是在当前的localhost目录。导致请求没有代理成功
POST http://localhost:8080/api/data 400 (Bad Request)
已经解决
如何解决的?能否写出来?我也遇到同样的问题了。多谢!
我也搞定了,内容是这样的: config.devServer = { proxy: { '/api': { target: 'http://127.0.0.1:8050' } } }
是在webpack.dev.config.js里配置的吗?配置后不成功呀,config未定义
module.exports = merge(webpackBaseConfig, {
devtool: '#source-map',
output: {
publicPath: '/dist/',
filename: '[name].js',
chunkFilename: '[name].chunk.js'
},
plugins: [
new ExtractTextPlugin({
filename: '[name].css',
allChunks: true
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
filename: 'vendors.js'
}),
new HtmlWebpackPlugin({
filename: '../index.html',
template: './src/template/index.ejs',
inject: false
})
],
devServer: {
proxy: {
'/api': {
target: 'http://127.0.0.1:8080'
}
}
}
});
我是从本地的127.0.0.1:8081代理到127.0.0.1:8080,代理成功了,但是老实讲不知道为什么写个devServer可以。。。
https://webpack.js.org/configuration/#options 看下这个链接,webpack的配置大全,里边就有devServer的配置,这样就了然了。 https://doc.webpack-china.org/configuration/dev-server/#devserver-proxy