skip to Main Content

I am trying to serve images.

this is the endpoint (which I have modified to use "/backend" and many other combinations that did not work):

app.use('/uploads', express.static(__dirname + '/uploads'));

this is "__dirname + ‘/uploads’":

C:Usersjack7OneDriveDocumentsProgramming Projects30 days of ReactBlogProjectbackend/uploads

this is the path to my img:

C:Usersjack7OneDriveDocumentsProgramming Projects30 days of ReactBlogProjectBackEnduploads71d1d5f8cb5ed5a9401764de2cf0b546.jpg

this is my request url and error:

GET http://localhost:5000/backend/uploads/71d1d5f8cb5ed5a9401764de2cf0b546.jpg 404 (Not Found)

The only possible issues I have found are related to casing and reconciliation of the use of both forward and back slashes in paths, which seems to be auto-corrected anyways. I am not really sure how much context and what context to use for explaining this problem, so please let me know. I hope there is something obviously wrong with the code and logic I provided.

2

Answers


  1. It looks like you’re requesting localhost:5000/backend/uploads/, but you’re serving uploads on the /uploads URL path.

    You should just need to send a GET request to localhost:5000/uploads/71d1d5f8cb5ed5a9401764de2cf0b546.jpg.

    Hope that helps!

    Login or Signup to reply.
  2. Instead of :

     app.use('/uploads', express.static(__dirname + '/uploads'));
    

    Try :

    const path = require('path');
    app.use('/uploads', express.static(path.join(__dirname, 'uploads')))
    

    From https://www.geeksforgeeks.org/express-js-express-static-function/


    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search