req.on('response') not working in plugins
See original GitHub issueI’m creating a custom policy plugin to transform response. Since EG middleware is essentially express middleware, I tried to add a listener for response
event:
policy: () => (req, res, next) => {
req.on('response', () => {
}
}
But the response
event is never triggered, and neither is any of the other events for HttpClientRequest
(https://nodejs.org/api/http.html#http_class_http_clientrequest).
For context, I’m creating a plugin to transform the response body. But the response I’m getting from the server is in chunks (header set to Transfer-Encoding: chunked
)
Overriding res.write
, as described here doesn’t work because I have to join all the chunks before doing any transformation.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Node.js Vanilla Routes Req.on not sending response
You need to change the logic inside your post handler a bit. In the .on("data") callback simply gather all the chunks that are...
Read more >request.Response.on JavaScript and Node.js code examples
Best JavaScript code snippets using request.Response.on(Showing top 15 results out of 315) · src/ctrls/url.js/urlCtrl · NluApiSample.js/request.get.on · test/test- ...
Read more >Develop custom plugins | Apigee Edge
The onend_response function is called when the last byte of response data is received. Note: Important: The next() function is required by all...
Read more >5 ways to make HTTP requests in Node.js - LogRocket Blog
You should have Node.js running on your machine (maybe as a Docker ... ability to automatically transform request and response data to JSON....
Read more >HTTP Request - Jenkins Plugins
For example, responses such as 404 and 500 could make the job fail. When a job fails it will log the response to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@XVincentX thanks for looking into this.
I have a policy plugin which is based on the response-transformer. I’m overriding the
res.write
as well.The difference is that the service sending the response is sending it in chunks, so the
data
inis an invalid JSON. In this specific case the response from the service is sent in 3 chunks, and all 3 chunks combined make a valid JSON. But the policy is trying to parse the JSON data for each chunk which obviously fails.
I thought the
req
in this case is the request that’s being proxied to my service and I could just add areq.on('response')
event listener and wait for the full response to be received before doingJSON.parse()
. But like you pointed out, req is an instance ofIncomingMessage
so there’s noresponse
event. What would you recommend in this case?@XVincentX can you give an example, I really have no idea how to process with this !