serve-index

NPM Version NPM Downloads Linux Build Windows Build Test Coverage

Serves pages that contain directory listings for a given path.为包含给定路径的目录列表的页面提供服务。

Install安装

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-index

API

var serveIndex = require('serve-index')

serveIndex(path, options)

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/dirreq.url将显示为'public/some/dir'If you are using something like express, you can change the URL "base" with app.use (see the express example).如果您使用的是express之类的工具,则可以使用app.use更改URL“base”(参见express示例)。

Options

Serve index accepts these properties in the options object.服务索引在选项对象中接受这些属性。

filter

Apply this filter function to files. 将此筛选功能应用于文件。Defaults to false. 默认为falseThe 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是文件所在的绝对路径(因此,列表所在的目录)。

hidden

Display hidden (dot) files. 显示隐藏(点)文件。Defaults to false.默认为false

icons

Display icons. 显示图标。Defaults to false.默认为false

stylesheet

Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.CSS样式表的可选路径。默认为内置样式表。

template模板

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:当给定字符串时,该字符串用作要加载的文件路径,然后在模板中替换以下标记:

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:以下是当地人提供的信息:

view

Display mode. 显示模式。tiles and details are available. tiles(方块平铺)和details(详细)是可用的。Defaults to tiles.默认为title

Examples例子

Serve directory indexes with vanilla node.js http server使用香草 node.js http服务器提供目录索引

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)

Serve directory indexes with express使用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. The Silk icons are created by/copyright of FAMFAMFAM.