跟随 loadImage() 函数的重定向

Follow redirect on loadImage() function

我在尝试使用 loadImage() 函数时总是出现错误 "Error: Server responded with 301"

我在函数中提供的 URL 从 http 重定向到 https,但函数没有遵循重定向,而是返回了该错误。

async run(msg, { item }) {
    const canvas = Canvas.createCanvas(330, item.height);
    const ctx = canvas.getContext('2d');
    ctx.fillStyle = 'rgb(54, 57,62)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    const iconImg = await Canvas.loadImage(item.icon); // <---
    ctx.drawImage(iconImg, 0, 0);
    const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'image.png');
    msg.channel.send(attachment);
}

有什么方法可以使该函数遵循 301 重定向的重定向而不是返回该错误?

这将在下一版本 (https://github.com/Automattic/node-canvas/pull/1398) - 2.4.2 或 2.5.0 中修复。

在那之前,您必须手动发出 HTTP 请求并遵循重定向。