Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

req.on('response') not working in plugins

See original GitHub issue

I’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 (

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:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

jay-finncommented, Aug 17, 2019

@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 in

return (req, res, next) => {
  res.write = (data) => { 

is 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 a req.on('response') event listener and wait for the full response to be received before doing JSON.parse(). But like you pointed out, req is an instance of IncomingMessage so there’s no response event. What would you recommend in this case?

xinjiguaikecommented, Sep 14, 2020

I guess you could be using a streaming serialiser (you can pick up any on NPM) to accumulate the buffer and then send it once calling the original _write.

@XVincentX can you give an example, I really have no idea how to process with this !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found