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
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!
Instead of :
Try :
From https://www.geeksforgeeks.org/express-js-express-static-function/
Maybe the set up is not right, your mixing windows and linux paths. (edit :mixing forward and back slashes is fine, backend takes care of it)
Also try not to keep any spaces in your folder names. Try this in
c:progreactproject2
or some such folderIn linux like programs path case has to be preseved. So use what is the actual captitilzation, copy it.
Tip : To copy path in windows: can press shift, right click and go down to Copy path i windows see https://www.thewindowsclub.com/path-copy-copy-lets-you-copy-file-paths or https://www.sony.com/electronics/support/articles/00015251