-2

SO my problem is images that i upload are set in the database and saves in vs code in uploads folder but i dont know how to display them on front end i have tried something but furthur needs to know the procedure

 <script>
                      
  axios
    .get(
      "http://localhost:8000/hotels")
    .then((response) => {
      console.log(response.data)
      for (let i = 0; i < response.data.length; i++) {
              document.getElementById('data').innerHTML+=`<tr class="odd">
                      <td class="dtr-control sorting_1" tabindex="0">${response.data[i].id}</td>
                      <td><img src='${response.data[i].image}'></td>
                      <td>${response.data[i].name}</td>
                      <td>${response.data[i].description}</td>
                      <td>${response.data[i].price}</td>
                      <td >${response.data[i].address}</td>`                                         
      }
     
  })
  
    .catch((error) => {
      console.log(error);
    });

                      </script>

So here you can see when i upload data the images are stored in the uploads folder All we need is to display image on front end somehow

2 Answers2

1

Your /uploads folder is not hosted like other routes. you need to host it in order to resolve the URL. Here is how you can using express.

EDIT: Explanation In your app.js/index.js(where you have initialized app()) write the code below

const path = require('path')
app.use('/uploads',
  express.static(path.join(__dirname,
    'uploads')))
Also i believe the question is similar to How to serve an image using nodejs
Sardar
  • 186
  • 5
-3

You can use instead of

<td><img src='${response.data[i].image}'></td>

this line

<td><img src='${new URL("uploads/"+response.data[i].image, window.location.origin).toString()}'></td>
Hichaaaaam
  • 39
  • 3
  • GET http://localhost:8000/uploads/Screenshot%20(3).png 404 (Not Found) same issue again with this code as well @hichaaaam – f180111 Yousaf Zahid Jan 05 '23 at 11:01
  • Let's try another way. Go to the upload folder, then open one of the images in that folder using Google Chrome or another browser. Then, send us the URL that you see. – Hichaaaaam Jan 05 '23 at 11:04
  • ok let me do it file:///C:/Users/Unknown/Desktop/ecommerce_website/uploads/Screenshot%20(3).png – f180111 Yousaf Zahid Jan 05 '23 at 11:05
  • any issue you see till now? – f180111 Yousaf Zahid Jan 05 '23 at 11:09
  • To be honest, I haven't found another solution, but the image may not be accessible due to permissions or security restrictions. You should check to see if there are any permissions that you need to fulfill in order to access the image. – Hichaaaaam Jan 05 '23 at 11:16
  • No issue brother, I dont think if there should eb any permission issue because its just a local host project running locally.There should be anything that i could possibly be missing but let me hit and trial it hope so it can come with a possible outcome – f180111 Yousaf Zahid Jan 05 '23 at 11:21