node.js Express.js - 显示文件/目录列表的任何方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6294186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:16:35  来源:igfitidea点击:

Express.js - any way to display a file/dir listing?

node.jsexpress

提问by balupton

With Express.jsis 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?

随着Express.js是有显示的文件/目录列表就像当你访问它没有一个索引文件目录的URL Apache没有办法-所以它显示的所有目录中的内容的列表?

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! :)

干杯伙计们,你摇滚!:)

回答by yonran

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.

有一个名为directory( source) 的全新默认Connect 中间件用于目录列表。它有很多风格,并有一个客户端搜索框。

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);

回答by jbll

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

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

Then, for example, to display the file/dir listings in a directory at the root of the app called videoswould look like:

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

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

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

回答by Talespin_Kit

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')));

回答by Xin

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'

这将为您完成工作:(新版本的 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

回答by Matteljay

Built-in NodeJS module fsgives a lot of fine-grained options

内置的 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)
})

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

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