http-proxy-middleware
http-proxy-middleware copied to clipboard
Error fetching proxied request: unexpected end of file
yarn why http-proxy-middleware OR npm ls http-proxy-middleware output (mask private folder names with *****)
% npm ls http-proxy-middleware
***/my-node-proxy
└── [email protected]
- [X] I understand project setup issues should be asked on StackOverflow or in GitHub Discussions.
- [X] I updated to latest
http-proxy-middleware.
Describe the bug (be clear and concise)
When receiving a 204 (with no body) proxyResponse, the endpoint using http-proxy-middleware returns a 200 response with body: Error fetching proxied request: unexpected end of file
The error seems to happen in responseInterceptor, maybe in the on('data) handler?:
function responseInterceptor(interceptor) {
return async function proxyRes(proxyRes, req, res) {
const originalProxyRes = proxyRes;
let buffer = Buffer.from('', 'utf8');
// decompress proxy response
const _proxyRes = decompress(proxyRes, proxyRes.headers['content-encoding']);
// concat data stream
_proxyRes.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));
[...]
_proxyRes.on('error', (error) => {
res.end(Error fetching proxied request: ${error.message});
});
};
}
Step-by-step reproduction instructions
Create an endpoint with a middleware with a responeInterceptor like this:
app.use('/abc/someurl', createProxyMiddleware({
target: 'sometarget',
changeOrigin: true,
pathRewrite: {
[`^/abc`]: '',
},
selfHandleResponse: true,
onProxyRes: responseInterceptor(async (responseBuffer: Buffer) => {
return responseBuffer;
}),
}
and use it to call an endpoint that returns a 204 response (without a body)
Expected behavior (be clear and concise)
endpoint should return the 204 response
What http-proxy-middleware configuration are you using?
see step-by-step reproduction
What OS/version and node/version are you seeing the problem?
macOS 11.5.2 (20G95)
npm -v
6.14.8
node -v
v14.15.1
Additional context (optional)
No response
Maybe I am using responseInterceptor incorrectly? I have tried returning the responseBuffer and also returning empty string but both show the same behaviour
this is the proxy response being handled:
statusCode: 204
headers:
access-control-allow-headers: authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction
access-control-allow-methods: GET
access-control-allow-origin: *
access-control-expose-headers:
activityid: asdfsadf-9e0f-4f02-9c08-c2cfe1032291
cache-control: proxy-revalidate
connection: keep-alive
content-type: application/json;charset=utf-8
date: Tue, 24 Aug 2021 13:16:53 GMT
custom-reqid: U-320:AFDF8B41-2E53-46C2-809C-599F06784DFC
strict-transport-security: max-age=15724800; includeSubDomains