Open cdisk opened 4 years ago
any updates about this Issue ? :/
Not sure if it's relevant, but I also had this error in Angular application, while trying to proxy POST request via pathRewrite
.
It appears that pathRewrite
works fine for GET requests.
pathRewrite: () => '/mocks/get-heroes-response.mock.json'
Yes, it could also be a function, not an object.
But for POST and PUT bypass
should be used instead.
bypass: (request) => {
if (request.method === 'POST') {
request.method = 'GET';
return '/mocks/create-hero-response.mock.json';
}
}
Using pathRewrite
for POST and PUT also works, but with error you sent.
After new research I figured out that bypass
suffers from https://github.com/webpack/webpack-dev-server/issues/829.
Luckily, you could substitute it with pathRewrite
like this.
// for non-GET requests
pathRewrite: (path, request) => {
request.method === 'GET';
return '/path/to/mock.json';
}
// for GET requests
pathRewrite: () => '/path/to/mock.json'
If you have multiple return
s though, you should wrap request.method === 'GET';
with e.g. if (request.method === 'POST') {}
to avoid infinite loop situations.
// for non-GET requests
pathRewrite: (path, request) => {
if (request.method === 'POST') {
request.method === 'GET';
return '/path/to/post-mock.json';
}
if (request.method === 'PUT') {
request.method === 'GET';
return '/path/to/put-mock.json';
}
}
Or, more elegant solution.
// for non-GET requests
pathRewrite: (path, request) => {
const post = request.method === 'POST';
const put = request.method === 'PUT';
if (post || put) {
request.method === 'GET';
}
if (post) {
return '/path/to/post-mock.json';
}
if (put) {
return '/path/to/put-mock.json';
}
}
Little update regarding this. we faced an issue with same error message using express with http-proxy-middleware
and the issue was that in implementation next()
was called twice, once in the middleware and the other one onError without return.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.
I want to use both
pathRewrite
andfollowRedirects
. I use the vue webpack config this funtions. But When I addfollowRedirects
config attribute,the console show me this error.Steps to reproduce
proxy
config tovue.config.js
file.pathRewrite
andfollowRedirects
toproxy
node.yarn serve
to start project.Here is my
vue.config.js
file:Here is the error message displayed in the console:
please help me.