I have a binary data(like image file) in Buffer object(not file), and want to serve the raw binary data to client through http.ServerResponse. How can I do it ?
-
Could you please make clear why the question is downvoted, if possible ?Tsuneo Yoshioka– Tsuneo Yoshioka2015-12-21 09:01:35 +00:00Commented Dec 21, 2015 at 9:01
-
I don't know why it is downvoted, but I was looking through Node's Response.js source and API, and I logged its keys, but I could not find a write method. What is the write method? Where did you learn of it?pward– pward2016-07-11 19:08:32 +00:00Commented Jul 11, 2016 at 19:08
-
The method is well documented in node js docs you can also find an explanation in the classic w3schoolsMark E– Mark E2020-06-15 01:38:38 +00:00Commented Jun 15, 2020 at 1:38
Add a comment
|
2 Answers
I managed to find out the answer. Just add "binary" encoding to both write() and end().
res.write(buffer,'binary');
res.end(null, 'binary');
Note that both "write" and "end" function requires the 'binary' encoding specified. Otherwise, the buffer is encoded as UTF-8. (So, JPEG header "ff d8 ff e0" will be "c3 bf c3 98 c3 bf c3 a0"...)