Экспресс-ответ с буфером в качестве изображения - PullRequest
0 голосов
/ 17 апреля 2019

Я хотел бы вернуть изображение, созданное на основе 64 Buffer из конечной точки Express, что работает ожидаемо, но в Chrome Devtools тип возвращаемого содержимого читается как тип Document.

enter image description here

const pixel = new Buffer(
  "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
  "base64"
);

app.get('/*.gif', async (request, response) => {

  try {
    const { u } = request.query;
    const { name: id } = path.parse(request.path);

    if (!u) throw new Error("No user found");
    if (!id) throw new Error("No id found");

    response.setHeader( "Content-Type", "image/gif")
    response.setHeader( "Content-Length", pixel.length,)
    response.setHeader( "Cache-Control", "no-cache, no-store, must-revalidate")
    response.setHeader( "Content-Type", "image/gif")
    response.send(pixel);


  } catch (error) {
     response.status(400);
      return response.send(error);
  }
});

1 Ответ

0 голосов
/ 17 апреля 2019

Все gif, похоже, передаются как документы в chrome, просто смотрите:

https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif

...