Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a binary Buffer to client through http.ServerResponse in Node.js

Tags:

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 ?

like image 426
Tsuneo Yoshioka Avatar asked Dec 21 '15 07:12

Tsuneo Yoshioka


1 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"...)

like image 96
Tsuneo Yoshioka Avatar answered Sep 19 '22 16:09

Tsuneo Yoshioka