我想通过 Http 响应发送 Buffer,但在客户端上我收到的是字符串而不是 Buffer。我使用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'
});
在上面的代码中,我的预期结果是 Buffer (),但我得到了字符串输出“Hello World”。
谁来帮帮我。提前致谢。
最佳答案
正如 Express 文档中所述,http://expressjs.com/en/api.html
When the parameter is a Buffer object, the method sets the Content-Type response header field to “application/octet-stream”
取决于您的浏览器,有些可能会将响应下载为文件(例如 Chrome),有些可能会读取流并直接显示内容(例如 IE11)。
关于node.js - Nodejs + Expressjs 将 http 响应作为缓冲区获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36571783/