具有此module.exports
文件,该文件可从API端点获取图像。然后将API结果解析为Blob。解析后,Blob对象如下所示:
Blob {
[Symbol(type)]: 'image/jpeg',
[Symbol(buffer)]:
<Buffer ff d8 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14
0d 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c 1c 20 24 2e 27 20 22 2c 23 1c
... > }
这是代码:
// Pre Configuration
const fetch = require('node-fetch')
module.exports = async (req, res, photoKey) => {
let photoUrl = null
const apiURL = "https://media.heartenly.com/stg/CACHE/sc_thumb"
const requestURL = `${apiURL}/${photoKey}`
const response = await fetch(requestURL)
const data = await response.blob()
console.log(data)
}
现在我要做的是返回返回的Blob的base64 URL格式,有什么想法吗?
答案 0 :(得分:1)
看着节点获取,看起来无法到达Blob缓冲区,因此,最好的选择是执行以下操作
换句话说:
const fetch = require('node-fetch');
module.exports = async (req, res, photoKey) => {
let photoUrl = null;
const apiURL = "https://media.heartenly.com/stg/CACHE/sc_thumb";
const requestURL = `${apiURL}/${photoKey}`;
const response = await fetch(requestURL);
const data = await response.buffer()
const b64 = data.toString('base64');
console.log(b64);
};