浏览 Node.js/express 中的静态文件目录树

发布于 2024-12-10 16:17:58 字数 469 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-12-17 16:17:58

因为您在放置时请求的

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

是目录列表请求,并且静态中间件似乎只提供文件而不是目录。您必须使用

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

这将允许您请求您正在讨论的 URI。

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.

请别遗忘我 2024-12-17 16:17:58

对于 Express 4,这看起来有点不同:

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

请在此处查看详细信息:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文