0

I have a POST API in Nodejs(Express), The API takes a lot of time to send back response(Heavy data processing).

When I cancel the request midway through Postman or close the page. (The API keeps processing the data in the node server, even though the response will not be collected by client.) How do I stop the internal API processing as well when the request is closed ?(Client stops the API)

1 Answers1

0

With express, you can try:

req.connection.on('close',function(){    
  // code to handle connection abort
  console.log('user cancelled');
});
Tushar Wasson
  • 494
  • 5
  • 10
  • I have tried this solution, But unfortunately with this code when API starts, it also executes this part of the code as well and the rest of the process is stopped even when client has not cancelled the API or stopped listening for response. – Sardeep Chabbra bluCursor May 18 '22 at 13:51