黑人生命也是命。
支持平等正義倡議.

serve-index

NPM Version NPM Downloads Linux Build Windows Build Test Coverage

提供包含指定路徑目錄清單的頁面。

安裝

這是 Node.js 模組,可透過 npm 註冊 取得。安裝時使用 npm install 指令

$ npm install serve-index

API

var serveIndex = require('serve-index')

serveIndex(path, options)

傳回中介軟體,用於提供給定 path 中目錄的索引。

path 是基於 req.url 值,因此 req.url'/some/dir,且 path'public',將會查看 'public/some/dir'。如果您使用類似 express 的東西,可以使用 app.use 變更 URL「基礎」(請參閱 express 範例)。

選項

提供索引會在選項物件中接受這些屬性。

filter

將此篩選函式套用至檔案。預設為 falsefilter 函式會針對每個檔案呼叫,其簽章為 filter(filename, index, files, dir),其中 filename 是檔案名稱,index 是陣列索引,files 是檔案陣列,而 dir 是檔案所在位置的絕對路徑(因此,是清單所屬的目錄)。

hidden

顯示隱藏的 (點) 檔案。預設為 false

icons

顯示圖示。預設為 false

stylesheet

指向 CSS 樣式表的選用路徑。預設為內建樣式表。

template

指向 HTML 範本或將會呈現 HTML 字串的函式的選用路徑。預設為內建範本。

當給定字串時,該字串會用作要載入的檔案路徑,然後在範本中取代下列權杖

  • {directory},其中包含目錄名稱。
  • {files},其中包含檔案連結的未排序清單的 HTML。
  • {linked-path},其中包含指向目錄的連結的 HTML。
  • {style},其中包含指定的樣式表和嵌入式影像。

當作為函式提供時,函式會以 template(locals, callback) 呼叫,且需要呼叫 callback(error, htmlString)。以下是提供的 locals

  • directory 是顯示的目錄(其中 / 是根目錄)。
  • displayIcons 是布林值,表示是否要呈現圖示。
  • fileList 是目錄中已排序的檔案陣列。陣列包含具有下列屬性的物件
    • name 是檔案的相對名稱。
    • stat 是檔案的 fs.Stats 物件。
  • pathdirectory 的完整檔案系統路徑。
  • style 是預設樣式表或 stylesheet 選項的內容。
  • viewNameview 選項提供的檢視名稱。
檢視

顯示模式。可用選項為 tilesdetails。預設為 tiles

範例

使用純粹的 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)

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

授權

MITSilk 圖示是由 FAMFAMFAM 創作/擁有版權。