0

I am working with on a user profile page with nodejs where the user can see his own avatar after login. The user has the posibility to upload the files to the uploads folder which is outside the regular htmlpath. I have the following folder structure:

  • Layouts
  • node_modules
  • public (all static files)
  • schema
  • uploads (user uploaded files)

       eps
         euro-pool-1501781922012.jpg
    
  • util

  • views

These are the settings for the htmlpath in app.js:

app.use(require('serve-static')(path.join(__dirname, 'public')));
app.use(require('method-override')());
app.use(express.static('public'));

on the client side i use this relative path to the image:

<img src="../uploads/eps/euro-pool-1501781922012.jpg" class="img-circle profile-avatar" alt="User avatar" data-pin-nopin="true"> 

i also tried

 <img src="./uploads/eps/euro-pool-1501781922012.jpg" class="img-circle profile-avatar" alt="User avatar" data-pin-nopin="true"> 

but when i navigate to the page it failed to show the image and i get the folowing error back:

GET http://192.168.2.20:3000/uploads/eps/euro-pool-1501781922012.jpg 404 (Not Found)

I have two questions:

  1. Is it posible to acces files outside the htmlpath?
  2. How can i fix this problem without moving the whole folder to the public folder?

Many thanks,

Erik

1
  • What do you mean by htmlpath? Do you mean your static public folder? Commented Aug 6, 2017 at 13:57

1 Answer 1

1

Yes, it's possible, if you don't want to restrict access to this folder, you could set up two different directories to serve static files:

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

In this case the path to the image should start from /uploads:

<img src="/uploads/eps/euro-pool-1501781922012.jpg" ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.