Nodejs + expressjs 获取 http 响应作为缓冲区

Sha*_* Us 4 httpresponse node.js express

我想通过 Http 响应发送一个缓冲区,但在客户端上我将它作为字符串而不是缓冲区接收。我使用expressjs路由器,如下代码

router.get('/', function(req, res, next) {
  const buf = new Buffer('Hello world');
  console.log(buf); // gives me <Buffer 48 65 6c 6c 6f 20 77 6f 72 6c 64>
  res.send(buf); // gives me 'Hello world' 
});
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我的预期结果是 Buffer () 但我得到了字符串输出 'Hello World.

有人帮帮我。提前致谢。

Ant*_*y C 5

如 Express 文档中所述,http://expressjs.com/en/api.html

当参数为Buffer对象时,该方法设置Content-Type响应头域为“application/octet-stream”

取决于您的浏览器,有些可能会将响应下载为文件(例如 Chrome),有些可能会读取流并直接显示内容(例如 IE11)。