浏览 Node.js/express 中的静态文件目录树
我有一个 public/ 目录,我将其设置为包含 Express 中的静态文件:
app.use(express.static(__dirname + '/public'));
其中有一个图像目录
/public/images
,并且有一个包含各种图像的深层子树。如果我输入图像的完整路径,加载就没有问题。
http://mysite.com/images/tiles/grass.png
当我只是转到一个 url 时,例如
http://mysite.com/images/tiles/
它只会给我当它尝试查找非静态路径时给出的错误,但该路径不存在。
如何才能使静态路径中的所有目录显示类似于 Apache 显示可导航目录结构的方式?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您在放置时请求的
是目录列表请求,并且静态中间件似乎只提供文件而不是目录。您必须使用
这将允许您请求您正在讨论的 URI。
Because what you're requesting when putting
is a directory listing request, and it seems that static middleware just serves files not directories. You have to use
This will let you request the URIs you're talking about.
对于 Express 4,这看起来有点不同:
请在此处查看详细信息:
https://github.com/expressjs/serve-index
For Express 4 this looks a little different:
Check here for details:
https://github.com/expressjs/serve-index