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

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


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


Using Axios for fetch the Binary data and send as Image

app.get(url, async (req, res) => {  let { thread, blob } = req.params;  try {    let { data, body, headers } = await axios.get(...);    res.end(data);  } catch (e) {    console.log("e", e);    res.send("error");  }});