Javascript Web包开发服务器代理服务器未连接
在我的webpack dev服务器中,我设置了一个代理,因此对以“api/”开头的路由的调用可以命中后端服务器,而不是webpack服务器。这是我的配置:Javascript Web包开发服务器代理服务器未连接,javascript,reactjs,webpack,webpack-dev-server,Javascript,Reactjs,Webpack,Webpack Dev Server,在我的webpack dev服务器中,我设置了一个代理,因此对以“api/”开头的路由的调用可以命中后端服务器,而不是webpack服务器。这是我的配置: devServer: { port: 8000, contentBase: 'dist', proxy: { '/api': { target: 'http://localhost:3000', changeOrigin: true } } } 在我
devServer: {
port: 8000,
contentBase: 'dist',
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true
}
}
}
在我的express服务器中,每当遇到示例api路由时,我都会记录一条简单的消息
const express = require('express')
const app = express()
app.get('api/hello', function (req, res) {
console.log('hitting API request');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
但是,似乎“/api”请求没有到达express服务器,因为在到达“localhost:8000/api/hello”时,控制台上没有记录任何消息。以下是我的webpack.config.js文件的剩余部分(如果有帮助):
const Path = require('path');
const Webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// plugins
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './client/index.html',
filename: 'index.html',
inject: 'body'
});
const CommonsChunkPluginConfig = new Webpack.optimize.CommonsChunkPlugin({name: 'vendor', filename: 'vendor.bundle.js'});
module.exports = {
entry: {
app: './client/index.js',
vendor: ['react', 'react-dom']
},
output: {
path: Path.resolve('dist'),
filename: 'index_bundle.js'
},
module: {
loaders: [
{ test: /\.js$/, loader:'babel-loader', exclude: /node_modules/ },
{ test: /\.jsx$/, loader:'babel-loader', exclude: /node_modules/ }
]
},
plugins: [
HtmlWebpackPluginConfig,
CommonsChunkPluginConfig
],
devServer: {
port: 8000,
contentBase: 'dist',
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true
}
}
}
}
你找到答案了吗?