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

557
Visualizações
How to display image from the Nodejs sever

I have images stored on the server and these images are stored by the path in the database and I have to display these images how can I do that

{
"_id": "61bb82935739a52a55316dc3",
"name": "Egg",
"type": "protein",
"image": "fooduploads\\Egg.png",
"__v": 0
}

this is how images are stored in database but how can I display them while I have to use these images in the Frontend. Should I have to make new route or I have to do something else

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can also make a route that takes in the img id, looks up the info in the database, reads the file from the filesystem and returns the image.

Here is some suedo code to explain

app.get('/img/:id', (req, res, next) => {
  // Code to talk to the database and get the json
  // getImageInfoFromDatabase(req.params.id)

  const dbResponse = {
    "_id": "61bb82935739a52a55316dc3",
    "name": "Egg",
    "type": "protein",
    "image": "fooduploads\\Egg.png",
     "__v": 0
  }
  
  const file = fs.readFileSync(path.join(__dirname, dbResponse.image)

  res.sendFile(file)
})

You can then in your front-end do: <img src='/img/61bb82935739a52a55316dc3 />

over 4 years ago · Santiago Trujillo Relatório
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