问题描述
使用 Express.js 有没有办法像 apache 那样在访问 URL 时显示文件/目录列表没有索引文件的目录 - 所以它显示所有目录内容的列表?
With Express.js is there a way to display a file/dir listing like apache does when you access the URL of a directory which doesn't have a index file - so it displays a listing of all that directories contents?
是否有我不知道的扩展或包可以执行此操作?还是我必须自己编写代码?
Is there an extension or package that does this which I don't know of? Or will I have to code this myself?
干杯伙计们,你们摇滚!:)
Cheers guys, you rock! :)
推荐答案
从 Express 4.x 开始,目录中间件不再与 express 捆绑在一起.您需要下载 npm 模块 serve-index.
As of Express 4.x, the directory middleware is no longer bundled with express. You'll want to download the npm module serve-index.
然后,例如,要在名为 videos
的应用程序根目录中显示文件/目录列表,将如下所示:
Then, for example, to display the file/dir listings in a directory at the root of the app called videos
would look like:
var serveIndex = require('serve-index');
app.use(express.static(__dirname + "/"))
app.use('/videos', serveIndex(__dirname + '/videos'));
这篇关于Express.js - 显示文件/目录列表的任何方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!