Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
How to display an image from a pipe stream in nodejs to an img tag

We have this code that pipes an image stored in MongoDB using GridFS

exports.getOneVerificationImageByFilename = async (req, res, next) => {
  const image = await gfs.files.findOne({ filename: req.params.image });
  try {
    const readStream = gfs.createReadStream(image.filename);
    readStream.on("error", function (err) {
      res.send("No image found with that title");
    });
    res.contentType("image/png");
    readStream.pipe(res);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
};

Im using react and I need to display this in an <img/> tag. This is how I called this endpoint

const getImage = async filename => {
    try {
        const response = await axios.get(`/image/${filename}`);
        return response;
    } catch (error) {
        throw new Error(error)
    }
}

Since the endpoint doesn't return any res, the response is empty. I then read about it that chunks of the data is being streamed. I found someone using this kind of syntax:

<img src="<%= url %>" />

However I dont understand it. I need to display the streamed chunks in an img tag

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda