1 回答
TA贡献1772条经验 获得超5个赞
在您的示例中,您使用了Base64encode已经抽出字符串的模块 - 这使得上面的示例失败(除非那是因为顶级等待)。您实际上不需要将块转换为字符串,因为它们已经是字符串,因此可以进行简单的串联。
事实上,仅使用 node.js 流,整个事情可能会简单 10 倍:
const Axios = require('axios')
const {PassThrough} = require("stream");
const url = 'https://unsplash.com/photos/AaEQmoufHLk/download?force=true';
(async function() {
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
});
// we just pipe the data (the input carries it's own encoding)
// to a PassThrough node stream that outputs `base64`.
const chunks = response.data
.pipe(new PassThrough({encoding:'base64'}));
// then we use an async generator to read the chunks
let str = '';
for await (let chunk of chunks) {
str += chunk;
}
// et voila! :)
console.log(str);
})();
添加回答
举报
