1252748
1252748

Reputation: 15362

Node is not serving static directory

I'm trying to show a directory of images when the directory is added to my app's URL. Given:

var serveindex = require('serve-index')
  , PORT = 8888;

neither:

var userUploadedImages = __dirname + '/userUploadedImages';
app.use(userUploadedImages, serveindex('/userUploadedImages'));

nor:

var userUploadedImages = 'http://localhost:' + PORT + '/userUploadedImages'; //this would for sure be more likely to work, but i tried both
app.use(userUploadedImages, serveindex('/userUploadedImages'));

work. The directory of images I have does not show up, just my index which would otherwise render if I were at the root with:

app.get('/', function(req, res){
    res.send('index.html');
});

I am using angular route, but do not have a route set up for userUploadedImages. I'm not getting any console errors in browser or in terminal where my server is running.

Upvotes: 2

Views: 56

Answers (1)

MinusFour
MinusFour

Reputation: 14423

Express app.use takes as the first argument a URI path under which the listing will be served and serve-index path takes a directory path to which they'll do the listings.

So in this example, it would serve the userUploadedImages directory in the same directory as the script file under the URL path /userUploadedImages:

var userUploadedImages = __dirname + '/userUploadedImages';
app.use('/userUploadedImages', serveindex(userUploadedImages));

So if you visit the server under /userUploadedImages you should see the listings of your userUploadedImages directory.

Upvotes: 1

Related Questions