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
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 />