Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

554
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!