How to apply createProxyMiddleware in webpack?
I have a webpack.config.js with default proxy:
// development config
require('dotenv').config()
const package = require('../../package.json')
const { merge } = require('webpack-merge')
const webpack = require('webpack')
const commonConfig = require('./common')
const agent = require('agentkeepalive')
module.exports = (webpackConfigEnv, argv) =>
merge(commonConfig(argv), {
mode: 'development',
entry: [
'react-hot-loader/patch', // activate HMR for React
'webpack-dev-server/client?http://localhost:3030',
'webpack/hot/only-dev-server',
'./index.tsx', // the entry point of our app
],
devServer: {
port: 3030,
hot: true, // enable HMR on the server
historyApiFallback: true,
proxy: {
'/api/*': {
target: 'http://foobar:8080/',
secure: false,
changeOrigin: true,
agent: new agent({
maxSockets: 100,
keepAlive: true,
maxFreeSockets: 10,
keepAliveMsecs: 100000,
timeout: 6000000,
freeSocketTimeout: 90000, // free socket keepalive for 90 seconds
}),
onProxyRes: (proxyRes) => {
var key = 'www-authenticate'
proxyRes.headers[key] =
proxyRes.headers[key] && proxyRes.headers[key].split(',')
},
},
},
},
// ... other code is omitted for the brevity
})
Now I would like to use this http-proxy-middleware because of this.
So I edit the above config to:
// ... other code is omitted for the brevity
proxy: createProxyMiddleware('/api/*', {
target: 'http://foobar:8080/',
secure: false,
changeOrigin: true,
agent: new agent({
maxSockets: 100,
keepAlive: true,
maxFreeSockets: 10,
keepAliveMsecs: 100000,
timeout: 6000000,
freeSocketTimeout: 90000, // free socket keepalive for 90 seconds
}),
onProxyRes: (proxyRes) => {
var key = 'www-authenticate'
proxyRes.headers[key] =
proxyRes.headers[key] && proxyRes.headers[key].split(',')
},
}),
},
// ... other code is omitted for the brevity
})
However, when I try to build my application, it throws an error:
> [webpack-cli] webpack Dev Server Invalid Options
> options.proxy should be {Object|Array} (https://webpack.js.org/configuration/dev-server/#devserverproxy)
Please, tell me how can is it possible to aplly createProxyMiddleware
for proxy
in webpack config?