I am writing my own HTTP module, if I need to response with a binary file, e.g. .jpg,
I load the file using: body = fs.createReadStream(pathOfFile).
When I generate the response I use:body.pipe(socket);
But as its HTTP response, I had like to add a Content-Length header.
I couldn’t find an easy way to do it, fs.stat doesn’t give the result immediately but just after I called pipe.
Anyway to know what to send in Content-Length header. ?
Thanks.
Well you should only send the response and pipe the file after you get the size with fs.stat, like so: