0

I want to serve image from mongodb order product schema. I am uploading the image using fs and saving the relative path. I do not want to use multer. Here is my data that I am sending into the emailtemplate

"product": {
                        "id": "64b104a01feb47555623d9cd",
                        "productName": "Sample Product",
                        "description": "This is a sample product description.",
                        "origin": "Sample Origin",
                        "images": [
                            "images/2b298231f64df75fc32007b0d366a382.jpg",
                            "images/348c013c9965d6bee382df6f776794b6.webp",
                            "images/d89a7b958585f0850f0aee511d7aef2e.jpeg"
                        ],
                        "quantity": 28,
                        "price": 29.99,
                        "category": "64b7967036ecd422ea03b121",
                        "tags": "sample, shoe, electronics",
                        "link": "https://example.com/sample-product",
                        "status": "active"
                    },

I want the image to be served in the ejs file. The ejs is accessing the file like this:

      <tr>
        <td>
          <img class="product-image" src="<%= serverLink + order.products[i].product.images[0] %>"
            alt="<%= order.products[i].product.productName %>">
          <%= serverLink + order.products[i].product.images[0] %>
            <%= order.products[i].product.productName %>
        </td>
        <td>
          <%= order.products[i].productQuantity %>
        </td>
        <td>$<%= order.products[i].product.price * order.products[i].productQuantity %>
        </td>
        <!-- <td>
          <%= order.requests[i] ? order.requests[i].requestQuantity : '-' %>
        </td> -->
      </tr>
      <% } %>

In the mail the image is breaking. I want to create an api that will take the image id then it will take the image from the relative path then serve it. It may not work in the localhost but it should work after deploying

1 Answers1

0

why don't you save images in public folder and serve those with express static files serving.

Suresh D
  • 85
  • 10
  • It is in public folder and if I serve those with static files serving then I can access them from the localhost but it does not occur in the mail template in the ejs file – Yeasir Hossain Jul 25 '23 at 09:55