Express.js - 有什么方法可以显示文件/目录列表?

发布于 2024-11-14 20:06:44 字数 182 浏览 2 评论 0原文

使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

原来分手还会想你 2024-11-21 20:06:44

从 Express 4.x 开始,目录中间件不再与 Express 捆绑在一起。您需要下载 npm 模块 serve-index

然后,例如,要显示应用程序根目录中名为 videos 的文件/目录列表,将如下所示:

    var serveIndex = require('serve-index');

    app.use(express.static(__dirname + "/"))
    app.use('/videos', serveIndex(__dirname + '/videos'));

As of Express 4.x, the directory middleware is no longer bundled with express. You'll want to download the npm module serve-index.

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'));
又爬满兰若 2024-11-21 20:06:44

有一个全新的默认 连接名为 directory 的中间件 (< a href="https://github.com/senchalabs/connect/blob/directory-folders/lib/middleware/directory.js" rel="nofollow noreferrer">源代码)用于目录列表。它有很多风格并且有一个客户端搜索框。

var express = require('express')
  , app = express.createServer();

app.configure(function() {
  var hourMs = 1000*60*60;
  app.use(express.static(__dirname + '/public', { maxAge: hourMs }));
  app.use(express.directory(__dirname + '/public'));
  app.use(express.errorHandler());
});

app.listen(8080);

There's a brand new default Connect middleware named directory (source) for directory listings. It has a lot of style and has a client-side search box.

var express = require('express')
  , app = express.createServer();

app.configure(function() {
  var hourMs = 1000*60*60;
  app.use(express.static(__dirname + '/public', { maxAge: hourMs }));
  app.use(express.directory(__dirname + '/public'));
  app.use(express.errorHandler());
});

app.listen(8080);
[浮城] 2024-11-21 20:06:44

以下代码将同时服务于目录和文件

var serveIndex = require('serve-index');
app.use('/p', serveIndex(path.join(__dirname, 'public')));
app.use('/p', express.static(path.join(__dirname, 'public')));

The following code will serve both directory and files

var serveIndex = require('serve-index');
app.use('/p', serveIndex(path.join(__dirname, 'public')));
app.use('/p', express.static(path.join(__dirname, 'public')));
少钕鈤記 2024-11-21 20:06:44

这将为您完成这项工作:(新版本的express需要单独的中间件)。例如,您将文件放在“files”文件夹下,并且希望 url 为“/public”

var express = require('express');
var serveIndex = require('serve-index');
var app = express();

app.use('/public', serveIndex('files')); // shows you the file list
app.use('/public', express.static('files')); // serve the actual files

This will do the work for you: (new version of express requires separate middleware). E.g. you put your files under folder 'files' and you want the url to be '/public'

var express = require('express');
var serveIndex = require('serve-index');
var app = express();

app.use('/public', serveIndex('files')); // shows you the file list
app.use('/public', express.static('files')); // serve the actual files
慕烟庭风 2024-11-21 20:06:44

内置 NodeJS 模块 fs 提供了很多细粒度的选项

const fs = require('fs')

router.get('*', (req, res) => {
    const fullPath = process.cwd() + req.path //(not __dirname)
    const dir = fs.opendirSync(fullPath)
    let entity
    let listing = []
    while((entity = dir.readSync()) !== null) {
        if(entity.isFile()) {
            listing.push({ type: 'f', name: entity.name })
        } else if(entity.isDirectory()) {
            listing.push({ type: 'd', name: entity.name })
        }
    }
    dir.closeSync()
    res.send(listing)
})

请务必阅读路径遍历安全漏洞。

Built-in NodeJS module fs gives a lot of fine-grained options

const fs = require('fs')

router.get('*', (req, res) => {
    const fullPath = process.cwd() + req.path //(not __dirname)
    const dir = fs.opendirSync(fullPath)
    let entity
    let listing = []
    while((entity = dir.readSync()) !== null) {
        if(entity.isFile()) {
            listing.push({ type: 'f', name: entity.name })
        } else if(entity.isDirectory()) {
            listing.push({ type: 'd', name: entity.name })
        }
    }
    dir.closeSync()
    res.send(listing)
})

Please make sure to read up on path-traversal security vulnerabilities.

假装不在乎 2024-11-21 20:06:44

这段代码怎么样?
简单,可以下载文件。
我在此处找到。

var express    = require('express')
var serveIndex = require('serve-index')

var app = express()

// Serve URLs like /ftp/thing as public/ftp/thing
// The express.static serves the file contents
// The serveIndex is this module serving the directory
app.use('/ftp', express.static('public/ftp'), serveIndex('public/ftp', {'icons': true}))

// Listen
app.listen(3000)

How about this code?
Simple and can download files.
I found in here.

var express    = require('express')
var serveIndex = require('serve-index')

var app = express()

// Serve URLs like /ftp/thing as public/ftp/thing
// The express.static serves the file contents
// The serveIndex is this module serving the directory
app.use('/ftp', express.static('public/ftp'), serveIndex('public/ftp', {'icons': true}))

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