I have a node http server that's streaming a large amount of data from an upstream source to a client. My code looks like:
handleRequest = function(req, res) {
var readable_stream = fetchDataFromUpstream();
res.statusCode = 200;
readable_stream.pipe(res);
}
My upstream source can emit errors. What's the proper way of handling them? Ideally I'd want to log the error and send a 500-status response to the client reporting the problem. However if the error happens mid-way through reading the original data, it's too late to set the correct status, right? I don't want to wait til I have all the data in memory before starting to send it to the client, since it's a lot of data. Is there a clean way of handling this?