Send binary buffer to client via http.ServerResponse in Node.js

I have binary data (like an image file) in a Buffer (not file) object and you want to serve raw binary data to the client through http.ServerResponse. How can i do this?

+6
source share
1 answer

I managed to figure out the answer. Just add the binary encoding for write () and end ().

res.write(buffer,'binary'); res.end(null, 'binary'); 

Note that the write and end functions require the specified binary encoding. Otherwise, the buffer is encoded as UTF-8. (So ​​the JPEG header "ff d8 ff e0" will be "c3 bf c3 98 c3 bf c3 a0" ...)

+10
source

All Articles