Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Serving Static Files in Express not working

I want:

I want to access static files in /uploads folder in the root of my project by visiting the URL (http://localhost:4002/uploads/test.jpg)

The issue:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am unable to access static files in uploads folder. Visiting the URL (http://localhost:4002/uploads/test.jpg) gives: Cannot GET /uploads/test.jpg

According to a tutorial I follow and to official docs it should work the way I have implemented it…

I have tried three methods:

  app.use(express.static('uploads'))
  app.use('/uploads', express.static('uploads')) 
  app.use('/uploads', express.static(path.join(__dirname, 'uploads')))

GitHub Repo Link

Documentation Link
image

>Solution :

In your github repo, there’s a tests.jpg, but NOT a test.jpg in the uploads folder so thus the URL in your question http://localhost:4002/uploads/test.jpg refers to a file that is not present in the uploads directory .

If you change the URL to match the filename that actually exists in the uploads folder, then

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

should work with the URL

http://localhost:4002/uploads/tests.jpg
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading