serve-index ミドルウェア
特定のパスのディレクトリ一覧を含むページを提供します。
インストール
これは、 Node.js モジュールで、
npm registry を介して利用できます。 インストールは
npm install コマンド:
$ npm install serve-indexAPI
var serveIndex = require('serve-index');serveIndex(パス, オプション)
指定された path 内のディレクトリのインデックスを提供するミドルウェアを返します。
pathはreq.url値に基づいています。/some/dir
pathのreq.urlはpublic/some/dir'になります。 If you are using
something like express, you can change the URL “base” with app.use (see
the express example).
オプション
Serve indexはoptionsオブジェクトでこれらのプロパティを受け付けます。
フィルター
このフィルタ機能をファイルに適用します。 デフォルトは 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 です。
アイコン
アイコンを表示 デフォルトは false です。
stylesheet
CSS スタイルシートへのオプションのパス。 デフォルトはビルトインスタイルシートです。
テンプレート
HTMLテンプレートまたはHTML 文字列をレンダリングする関数への任意のパス。 デフォルトはビルトインテンプレートです。
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) を呼び出す必要があります。 以下はローカルで提供される
です。
directoryは表示されているディレクトリです(/がルートです)。displayIconsは、アイコンをレンダリングするかどうかを指定するためのブール値です。fileListはディレクトリ内のファイルのソートされた配列です。 配列には、以下のプロパティを持つ オブジェクトが含まれています。nameはファイルの相対的な名前です。statはファイルのfs.Statsオブジェクトです。
pathはdirectoryへの完全なファイルシステムのパスです。styleはデフォルトのスタイルシートまたはstylesheetオプションの内容です。viewNameは、viewオプションによって提供されるビュー名です。
表示
表示モード tilesとdetailsがあります。 デフォルトは tiles です。
例
vanilla 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 filesvar serve = serveStatic('public/ftp');
// Create servervar 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); });});
// Listenserver.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 directoryapp.use('/ftp', express.static('public/ftp'), serveIndex('public/ftp', { icons: true }));
// Listenapp.listen(3000);