I am using webpack-dev-server as a proxy to my original API URL to avoid CORS related issues and dynamically pointing to Dev, UAT, Mock server URL based on the environments.
For Mock dev - I prefer to use the JSON from the local file system and it servers its purpose for all the GET request.
How to to add support for 'POST, PUT, DELETE' so that the local JSON can be served directly?
One possible approach may be - Overriding the HTTP Request methods to GET as I am not passing any payload. But I can't find any configuration for that.
Below is the current configuration :
if (IS_MOCK_SERVER) {
devServer.proxy = {
'/api': {
target: 'http://localhost:9090/data',
secure: false,
pathRewrite: function(req, options) {
return req + '.json'
}
}
}
} else {
devServer.proxy = {
'/api': {
target: 'http://dev-server-url.com',
secure: false
}
}
}
I tried digging into the official documents, but didn't get much support on this topic.
The Webpack dev server makes use of http-proxy-middleware to optionally proxy requests to a separate, possibly external, backend server.
As the Webpack dev server internally makes use of http-proxy-middleware to optionally proxy requests to other servers.
So, I opened a ticket at : https://github.com/chimurai/http-proxy-middleware/issues/151
Would be looking for a better solution in future.
But for time being, I found one workaround for the requirement/problem by Overriding the HTTP Method in API Util itself before passing it to Webpack-dev-server.
// Convert all the Request Method to Get ***ONLY IF*** MOCK-Server is running
if (process.env.IS_MOCK_SERVER) {
payload.method = 'get';
}