0

If I encounter an error after headers have been sent I need to be able to "kill" the connection in order to signal to the client that something went wrong. How do I do this i NodeJS/ExpressJS?

function (req, res, next) {
  createStream()
    .on('error', () => {
       if (res.headersSent) {
          /* KILL CONNECTION */
       } else {
          res.sendStatus(500)
       }
    })
    .pipe(res)
}
ronag
  • 49,529
  • 25
  • 126
  • 221
  • 1
    Possible duplicate of [Node.js Error: Can't set headers after they are sent](http://stackoverflow.com/questions/7042340/node-js-error-cant-set-headers-after-they-are-sent) – Nir Levy May 29 '16 at 19:51
  • @NirLevy: I know you can't set headers after they are sent. That the whole point of the question. I want to kill the connection... how is it a duplicate? – ronag May 29 '16 at 20:15
  • Maybe duplicate here: [Can I cancel a HTTP response after headers are sent?](http://stackoverflow.com/questions/31854487/can-i-cancel-a-http-response-after-headers-are-sent) – bloodyKnuckles May 29 '16 at 20:19
  • @bloodyKnuckles: Yep, thanks! – ronag May 29 '16 at 20:20

0 Answers0