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" ...)
source share