Node.jsでも同様

ローカルのpngを転送する場合

fsを使えるようにする

package.jsonに以下の設定を追加する

  "browser":{
    "fs": false
  },

streamを作成して応答する

const Readable = require("stream").Readable;
const img = fs.readFileSync(process.cwd() + "/public/img.png");
const stream = Readabl]e.from(img);
response.send(stream);

ローカルのsvgを転送する場合

// svg画像のテキスト
const svgImg = `<svg>.........</svg>`

// streamを作成
const Readable = require("stream").Readable;
const stream = new Readable();
stream._read = () => {};
stream.push(noImageSvg);
stream.push(null);

// content typeをヘッダーに設定し応答する
response.setHeader("Content-Type", "image/svg+xml");
response.send(s);

外部URLの画像を転送する場合

const res = await fetch("https://hogehoge.com/hoge.png");
const blob = await res.blob();
const contentType = res.headers.get("Content-Type");

response.setHeader("content-Type", contentType);
response.send(blob.stream());