Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browsing static file directory tree in Node.js/express

I have a public/ directory that I have set up as containing static files in express:

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

It has an images directory in it

/public/images

And that has a deep subtree of various images. If I put in the full path to the image, it loads with no problem.

http://mysite.com/images/tiles/grass.png

When I just go to a url such as

http://mysite.com/images/tiles/

It just gives me the error that it gives when it tries to find a non-static path, but the path doesn't exist.

How can I make it so all directories in my static path show something similar to the way Apache shows the navigable directory structure?

like image 983
RobKohr Avatar asked Oct 18 '11 11:10

RobKohr


2 Answers

Because what you're requesting when putting

http://mysite.com/images/tiles/

is a directory listing request, and it seems that static middleware just serves files not directories. You have to use

app.use(express.directory(your_path));
app.use(express.static(your_path));

This will let you request the URIs you're talking about.

like image 150
erick2red Avatar answered Oct 14 '22 18:10

erick2red


For Express 4 this looks a little different:

var directory = require('serve-index');
app.use(directory(your_path));

Check here for details:

https://github.com/expressjs/serve-index

like image 22
Jan Petzold Avatar answered Oct 14 '22 16:10

Jan Petzold