I try to transfer some sound to my server and then transfer it to the service specified by the user, the user will provide me with someHostName , which sometimes cannot support this type of request.
My problem is that when this happens, clientRequest.on('end',..) never starts, I think this is because it was sent to someHostReq , which got messed up when someHostName is "wrong".
My question is:
Is there anyway that I can still have clientRequest.on('end',..) , even if the clientRequest thread has something wrong?
If not: how did I find that something happened to someHostReq "immediately"? someHostReq.on('error') does not start, except after a while.
code:
someHostName = 'somexample.com' function checkIfPaused(request){//every 1 second check .isPaused console.log(request.isPaused()+'>>>>'); setTimeout(function(){checkIfPaused(request)},1000); } router.post('/', function (clientRequest, clientResponse) { clientRequest.on('data', function (chunk) { console.log('pushing data'); }); clientRequest.on('end', function () {//when done streaming audio console.log('im at the end'); }); //end clientRequest.on('end',) options = { hostname: someHostName, method: 'POST', headers: {'Transfer-Encoding': 'chunked'} }; var someHostReq = http.request(options, function(res){ var data = '' someHostReq.on('data',function(chunk){data+=chunk;}); someHostReq.on('end',function(){ console.log('someHostReq.end is called'); }); }); clientRequest.pipe(someHostReq); checkIfPaused(clientRequest); });
output:
if the hostname is correct:
pushing data . . pushing data false>>> pushing data . . pushing data pushing data false>>> pushing data . . pushing data console.log('im at the end'); true>>> //continues to be true, that fine
in case of an invalid host name:
pushing data . . pushing data false>>>> pushing data . . pushing data pushing data false>>>> pushing data . . pushing data true>>>> true>>>> true>>>> //it stays true and clientRequest.on('end') is never called //even tho the client is still streaming data, no more "pushing data" appears
if you think my question is a duplicate:
You can switch to the current mode by doing one of the following:
Adding a 'data' event handler to listen to data.
Call the resume () method to open the stream explicitly.
Call the pipe () method to send data to Writable.
source: https://nodejs.org/api/stream.html#stream_class_stream_readable