Serves pages that contain directory listings for a given path.为包含给定路径的目录列表的页面提供服务。
This is a Node.js module available through the npm registry. 这是一个Node.js模块,可通过npm注册表获得。Installation is done using the 使用npm install command:npm install命令完成安装:
$ npm install serve-indexvar serveIndex = require('serve-index')Returns middlware that serves an index of the directory in the given 返回为给定path.path中的目录提供索引的middlware。
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'. path基于req.url值,因此path为'public'的'/some/dir的req.url将显示为'public/some/dir'。If you are using something like 如果您使用的是express之类的工具,则可以使用express, you can change the URL "base" with app.use (see the express example).app.use更改URL“base”(参见express示例)。
Serve index accepts these properties in the options object.服务索引在选项对象中接受这些属性。
Apply this filter function to files. 将此筛选功能应用于文件。Defaults to 默认为false. 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).filter(filename, index, files, dir)为每个文件调用filter函数,其中filename是文件名,index是数组索引,files是文件数组,dir是文件所在的绝对路径(因此,列表所在的目录)。
Display hidden (dot) files. 显示隐藏(点)文件。Defaults to 默认为false.false。
Display icons. 显示图标。Defaults to 默认为false.false。
Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.CSS样式表的可选路径。默认为内置样式表。
Optional path to an HTML template or a function that will render a HTML string. HTML模板或呈现HTML字符串的函数的可选路径。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} {linked-path} {style} When given as a function, the function is called as 当作为函数给出时,该函数将以template(locals, callback) and it needs to invoke callback(error, htmlString). template(locals, callback)的形式调用,它需要调用callback(error, htmlString)。The following are the provided locals:以下是当地人提供的信息:
directory/ is the root)./是根目录)。displayIconsfileListnamestatfs.Stats object for the file.fs.Stats对象。pathdirectory.directory的完整文件系统路径。stylestylesheet option.stylesheet选项的内容。viewNameview option.view选项提供的视图名称。Display mode. 显示模式。tiles and details are available. tiles(方块平铺)和details(详细)是可用的。Defaults to 默认为tiles.title。
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)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)