'NodeJS http-proxy error response.end() not working

Here's the code:

 proxy.web(
  req,
  res,
  { changeOrigin: false, target: 'http://' + gotDomain.ip + ':' + gotDomain.port },
  function (error, req, res) => {
    console.log('Error')
    res.end('Error')
  }
)

If I stop the target server, the console log fires.

The response res.end('Error') does not work.

The request just hangs on the client side. I can run any code I want in the error callback and I can see the res variable is an http response object. I've tried adding:

 res.writeHead(500, {
    'Content-Type': 'text/plain'
  });

I've tried using the proxy.on('error'... setup and the results are exactly the same:

  • The callback runs
  • The res variable is a valid http response object
  • Whatever I put in res.end() doesn't get sent to the client
  • The client hangs forever waiting for a response.

I've checked this answer:

Node.js http-proxy: Error response not sent to client

I'm not sure how that solution could work because my proxy response doesn't have status() or send() methods. I get TypeError: res.status is not a function if I try those.

With http-proxy, I use end() whenever I'm returning a response.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source