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?
해결책
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.
다른 팁
For Express 4 this looks a little different:
var directory = require('serve-index');
app.use(directory(your_path));
Check here for details: