🌐 Nodejs.cn

serve-index 中间件

提供包含给定路径的目录列表的页面。

🌐 Serves pages that contain directory listings for a given path.

安装

🌐 Install

这是一个可以通过npm注册表获取的Node.js模块。安装可以使用npm install命令完成:

🌐 This is a Node.js module available through the npm registry. Installation is done using the npm install command:

Terminal window
npm install serve-index

Note

serve-index 不包含其自身的 TypeScript 类型定义。如果你使用 TypeScript,还需要作为开发依赖从 DefinitelyTyped 安装社区维护的类型:

Terminal window
npm install --save-dev @types/serve-index

API

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

serveIndex(path, options)

返回在指定 path 中提供目录索引的中间件。

🌐 Returns middlware that serves an index of the directory in the given path.

path 是基于 req.url 值的,所以 req.url'/some/dir 并且 path'public' 时会查看 'public/some/dir'。如果你使用类似 express 的东西,你可以用 app.use 更改 URL 的“基础”(参见 Express 示例)。

🌐 The path is based off the req.url value, so a req.url of '/some/dir with a path of 'public' will look at 'public/some/dir'. If you are using something like express, you can change the URL “base” with app.use (see the express example).

选项

🌐 Options

Serve index 在 options 对象中接受这些属性。

🌐 Serve index accepts these properties in the options object.

filter

将此过滤器函数应用于文件。默认值为 false。对于每个文件都会调用 filter 函数,其签名为 filter(filename, index, files, dir),其中 filename 是文件名,index 是数组索引,files 是文件数组,dir 是文件所在的绝对路径(因此也是列表所在的目录)。

🌐 Apply this filter function to files. Defaults to false. The filter function is called for each file, with the signature filter(filename, index, files, dir) where filename is the name of the file, index is the array index, files is the array of files and dir is the absolute path the file is located (and thus, the directory the listing is for).

hidden

显示隐藏的(点)文件。默认值为 false

🌐 Display hidden (dot) files. Defaults to false.

icons

显示图标。默认为 false

🌐 Display icons. Defaults to false.

stylesheet

CSS 样式表的可选路径。默认为内置样式表。

🌐 Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.

template

可选的 HTML 模板路径或将渲染 HTML 字符串的函数。默认为内置模板。

🌐 Optional path to an HTML template or a function that will render a HTML string. Defaults to a built-in template.

当给定一个字符串时,该字符串将被用作文件路径来加载,然后在模板中替换以下标记:

🌐 When given a string, the string is used as a file path to load and then the following tokens are replaced in templates:

  • {directory} 代表目录的名称。
  • {files} 带有文件链接的无序列表的 HTML。
  • {linked-path} 带有指向目录的链接的 HTML。
  • {style} 使用指定的样式表和嵌入的图片。

当作为函数提供时,该函数称为 template(locals, callback) 并且需要调用 callback(error, htmlString)。提供的本地变量如下:

🌐 When given as a function, the function is called as template(locals, callback) and it needs to invoke callback(error, htmlString). The following are the provided locals:

  • directory 是正在显示的目录(其中 / 是根目录)。
  • displayIcons 是一个布尔值,用于判断图标是否应该被渲染。
  • fileList 是目录中按顺序排列的文件数组。该数组包含具有以下属性的对象:
    • name 是该文件的相对名称。
    • stat 是该文件的 fs.Stats 对象。
  • pathdirectory 的完整文件系统路径。
  • style 是默认样式表或 stylesheet 选项的内容。
  • viewName 是由 view 选项提供的视图名称。
view

显示模式。可用的有 tilesdetails。默认值为 tiles

🌐 Display mode. tiles and details are available. Defaults to tiles.

例子

🌐 Examples

使用原生 node.js http 服务器提供目录索引

🌐 Serve directory indexes with vanilla node.js http server

var finalhandler = require('finalhandler');
var http = require('http');
var serveIndex = require('serve-index');
var serveStatic = require('serve-static');
// Serve directory indexes for public/ftp folder (with icons)
var index = serveIndex('public/ftp', { icons: true });
// Serve up public/ftp folder files
var serve = serveStatic('public/ftp');
// Create server
var server = http.createServer(function onRequest(req, res) {
var done = finalhandler(req, res);
serve(req, res, function onNext(err) {
if (err) return done(err);
index(req, res, done);
});
});
// Listen
server.listen(3000);

使用 Express 提供目录索引

🌐 Serve directory indexes with express

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

许可证

🌐 License

MIT。这些 Silk 图标由 FAMFAMFAM 创建/版权所有。